Reputation: 1
I am attempting to download a pdf file in ASP MVC using C#.
I have a UI-dialog with a button invoking the call to the controller:
"Download PDF": function () {
$.post(Urls.Action.DownloadPDF);
In the controller I am using a PDF converter to convert the html to PDF:
public ActionResult DownloadPDF()
{
string htmlToConvert = RenderViewAsString("~/Content/Eula.htm");
HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
byte[] pdfBuffer = htmlToPdfConverter.ConvertHtmlToMemory(htmlToConvert,null);
HttpContext.Response.AddHeader("content-disposition", "attachment; filename=Download.pdf");
FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
fileResult.FileDownloadName = "Download.pdf";
return fileResult;
}
The code runs through the method without issue in the debugger and the post returns successful but the browser is not downloading the PDF.
EDIT -
Key Value Response HTTP/1.1 200 OK Cache-Control private, s-maxage=0,private,no-store,no-cache,s-maxage=0,max-age=0,must-revalidate,proxy-revalidate,no-transform Pragma no-cache Content-Type application/pdf Expires -1 Server Microsoft-IIS/7.5 Set-Cookie FB; path=/; HttpOnly X-AspNetMvc-Version 3.0 content-disposition attachment; filename=Download.pdf Content-Disposition attachment; filename=Download.pdf X-AspNet-Version 4.0.30319 X-Powered-By ASP.NET X-Content-Type-Options nosniff X-XSS-Protection 1; mode=block X-UA-Compatible IE=edge,chrome=1 Date Fri, 07 Jun 2013 03:31:34 GMT Content-Length 81862
Upvotes: 0
Views: 13275
Reputation: 2017
I think your controller method should be something like this:
public FileContentResult DownloadPDF()
{
string htmlToConvert = RenderViewAsString("~/Content/Eula.htm");
HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
byte[] pdfBuffer = htmlToPdfConverter.ConvertHtmlToMemory(htmlToConvert,null);
return File(pdfBuffer, "application/pdf", "Download.pdf");
}
I haven't had a whole lot of experience with file downloads but similar code works for me to download excel files.
I found this post that specifically talks about downloading PDF's. It outline the difference between ActionResult
and FileResult
- ActionResult
doesn't contain the content type. It also explains the different results that build on FileResult
:
If the content you want to transfer is stored within a disk file, you can use the FilePathResult object. If your content is available through a stream you use FileStreamResult and you opt for FileContentResult if you have it available as a byte array. All these objects derive from FileResult and differ from one another only in the way that they write out data to the response stream.
EDIT: After a little searching, I believe I found the root of the problem. You may be able to use the same code, but you would need to call WriteFile
to write the content to the response stream - using File(...)
does that by default:
The result object that is prepared by this method is written to the response by the ASP.NET MVC framework when the object is executed.
See the MSDN docs for more info:
Upvotes: 1