簡體   English   中英

從aspx頁面下載PDF

[英]PDF download from aspx page

我有一個頁面,當用戶點擊按鈕時,動態生成PDF並供他們下載。

這是允許用戶下載pdf的代碼:

// Omitted code that generates the pdf bytes

response.ContentType = "application/octetstream";
response.AppendHeader("Content-Disposition", "attachment; filename=" + filename);
response.BinaryWrite(pdfBytes);
response.End();

在我的機器和許多其他使用Chrome,IE 7/8 / 9b和Firefox的混合物上,這是按預期工作的; 用戶單擊該按鈕,PDF將被下載。

在IE7的某些實例中,我們的用戶報告他們收到錯誤消息:

“Internet Explorer無法從site.com下載Publish.aspx

Internet Explorer無法打開此Internet站點。 請求的網站要么不可用,要么找不到。 請稍后再試”。

Publish.aspx是按鈕被駐留在,使頁面可用的頁面。 IE應該下載pdf。

以上代碼是否有任何錯誤可能導致某些機器上出現這種情況? 或者是特定的安全/操作系統/瀏覽器設置?

編輯:

這些是來自fiddler的響應頭:

HTTP/1.1 200 OK
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Content-Type: application/octetstream
Expires: -1
Server: Microsoft-IIS/7.5
Content-Disposition: attachment; filename=myPdf.pdf
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Date: Fri, 12 Nov 2010 09:48:06 GMT
Content-Length: 45772

這可能是因為正確的mime類型是application/octet-stream ,而不是application/octetstream

嘗試使用Response.OutputStream

filepath= Server.MapPath(filepath);
                FileStream strm = new FileStream(filepath, FileMode.Open, FileAccess.Read);

                byte[] fileByte = new byte[strm.Length];
                int x = strm.Read(fileByte, 0, fileByte.Length);

                Response.Clear();
                Response.AddHeader("Accept-Header", fileByte.Length.ToString());
                Response.AddHeader("Content-Disposition","inline; filename=" + filename);
                Response.ContentType = "application/pdf";
                Response.OutputStream.Write(fileByte, 0, fileByte.Length);
                Response.Flush();
                strm.Close();

並且您的內容類型必須是=“application / pdf”

最近我碰到了同樣的錯誤。 在我的情況下,我使用https而不是緩存。 IE中的安全功能似乎是不下載文件。 來自EricLaw的IEInternals:

如果用戶嘗試通過HTTPS連接下載*文件,則任何阻止緩存的響應頭都將導致文件下載過程失敗。

http://blogs.msdn.com/b/ieinternals/archive/2009/10/02/internet-explorer-cannot-download-over-https-when-no-cache.aspx

Nicolas是正確的,“octetstream”(沒有破折號)不是已知的MIME類型

我建議使用application/pdf

如果你使用response.TransmitFile / response.WriteFile會有所作為嗎?

TransmitFile(MSDN)
WriteFile的(MSDN)

好的,我已將內容類型更正為application / octet-stream並更改了緩存。 它似乎是一個IE + SSL問題,因此我會在今晚晚些時候部署它時發現它是否有效。 謝謝您的幫助。

google解決方案中發現類似問題:

Response.AppendHeader('Expires', 'Sun, 17 Dec 1989 07:30:00 GMT');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM