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?
– Jéf Bueno
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..
– Yendis
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 inHTTPGET
with the front-end callingwindow.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.– Daniel Nicodemos