[英]ITextSharp - Google Chrome Save Button Saves PDF as .ASPX page
我目前正在使用ITextSharp使用.Net 3.5 ASPX頁面創建PDF。 一切正常。.唯一的問題是在chrome(Version 42.0.2311.135)中,當我單擊“保存”按鈕或右鍵單擊並說“保存”,因為它試圖另存為.aspx頁。 使用Firefox或Internet Explorer時,我沒有遇到這些問題。 我嘗試禁用chrome PDF查看器,它只是自動將文件另存為aspx文件。 我在下面包括了我的代碼,該代碼是通過單擊按鈕觸發的。 我假設這是我處理響應的方式? 有任何想法嗎?
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();
}
更新:希望分享我的發現,以免使人頭疼。 添加Response.AddHeader(“ Content-Disposition”,“ inline; filename = myfilename.pdf”); 解決了大多數問題,但我仍然無法通過Google chrome瀏覽器中的“保存”按鈕遇到問題(打印效果很好,但保存提示用戶保存頁面,而不是pdf)。 單擊按鈕即可在.aspx頁上生成PDF。 生成后,我清除響應並將PDF轉儲到頁面。 頁面上的form元素沒有設置方法,因此它作為post發送(通過fiddler發現)。 該頁面是通過iframe共享的。 我在頁面上將方法更改為GET,現在一切都按預期工作。
嘗試:
Response.AddHeader("Content-Disposition", "attachment;filename=myfilename.pdf");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.