return/download pdf from backend to ajax

Asked

Viewed 122 times

0

I am converting an html to pdf, I managed to generate the pdf in the backend with Submit, but with ajax I cannot download the pdf.. How can I treat in ajax to download?

Code:

[HttpPost]
    [ValidateInput(false)]
    public ActionResult Export(string htmlContent)
    {
        var htmlToPdf = new HtmlToPdfConverter();

        var pdfContentType = "application/pdf";

        return File(htmlToPdf.GeneratePdf(htmlContent, null), pdfContentType, "TituloArquivo"+ DateTime.Now.ToString("ddMMyyyyHHmmss")+".pdf");

    }
  • Why use ajax if the server return will be a file?

  • because it will be an extra feature in the system for something specific, and should not influence the app as a whole.. I don’t know if I’m on the wrong track or if there’s another way..

  • Wouldn’t it be interesting to separate this method? One where it transforms html into pdf and another that downloads it. It would be a little more reusable. I have some methods calling via ActionResult with File return, but they are all done in HTTPGET with the front-end calling window.location.href. One method i Gero the pdf and returns the name of the file pro front and it with the name of the file downloads in another method. If you want I put this solution for you.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.