简体   繁体   中英

ITextSharp - Google Chrome Save Button Saves PDF as .ASPX page

I am currently using ITextSharp to create a PDF using a .Net 3.5 ASPX page. Everything works well.. the only issue is that in chrome(Version 42.0.2311.135) when I click the save button or right click and say save as it tries to save as an .aspx page. I don't experience these issues when using Firefox or Internet Explorer. I tried disabling the chrome PDF viewer and it just automatically saves the file as an aspx file. I've included my code below which is fired from a button click. I am assuming it is the way I am handling the response? Any ideas?

在此处输入图片说明

iTextSharp.text.Document d = new iTextSharp.text.Document(PageSize.A4, 20, 20, 20, 20);

using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{

 w = PdfWriter.GetInstance(d, stream);

 //add stuff to itext document

 d.Close();

 byte[] b = stream.ToArray();
 stream.Close();


 Response.Clear();
 Response.ContentType = "application/pdf";
 Response.BinaryWrite(b);
 HttpContext.Current.ApplicationInstance.CompleteRequest();

}

UPDATE: Wanted to share my findings in case it saves someone the headache. Adding Response.AddHeader("Content-Disposition", "inline;filename=myfilename.pdf"); fixed most of the issues but I was still experiencing a problem with the save button in the Google chrome viewer(print worked fine but save prompted the user to save the page and not the pdf). The PDF is being generated on a .aspx page through a button click. After it is generated I clear the response and dump the PDF to the page. The form element on the page didn't have a method set so it was being sent as post(found this out through fiddler). This page was being shared through an iframe. I changed the method to GET on the page and everything is now working as expected.

尝试:

Response.AddHeader("Content-Disposition", "attachment;filename=myfilename.pdf");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM