简体   繁体   English

从aspx页面下载PDF

[英]PDF download from aspx page

I have a page that when a user clicks a button, a PDF is dynamically generated and offered for them to download. 我有一个页面,当用户点击按钮时,动态生成PDF并供他们下载。

This is the code that's letting the user download the 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();

On my machine and many others using a mixture of Chrome, IE 7/8/9b and Firefox, this is working as expected; 在我的机器和许多其他使用Chrome,IE 7/8 / 9b和Firefox的混合物上,这是按预期工作的; the user clicks the button, the PDF gets downloaded. 用户单击该按钮,PDF将被下载。

On some instances of IE7, our users are reporting that they are getting an error message: 在IE7的某些实例中,我们的用户报告他们收到错误消息:

"Internet Explorer cannot download Publish.aspx from thesite.com “Internet Explorer无法从site.com下载Publish.aspx

Internet Explorer was not able to open this Internet site. Internet Explorer无法打开此Internet站点。 The requested site is either not available or cannot be found. 请求的网站要么不可用,要么找不到。 Please try again later". 请稍后再试”。

Publish.aspx is the page that the button is residing on, so that page is available. Publish.aspx是按钮被驻留在,使页面可用的页面。 IE should be downloading the pdf. IE应该下载pdf。

Is there anything that is wrong with the above code that could be causing this on certain machines? 以上代码是否有任何错误可能导致某些机器上出现这种情况? Or is it down to particular security / OS / browser settings? 或者是特定的安全/操作系统/浏览器设置?

EDIT: 编辑:

These are the response headers from fiddler: 这些是来自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

try using Response.OutputStream 尝试使用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();

and your content type must be ="application/pdf" 并且您的内容类型必须是=“application / pdf”

Very recently I bumped into the same error. 最近我碰到了同样的错误。 In my case I was using https and no caching. 在我的情况下,我使用https而不是缓存。 It seems to be a security feature in IE to not download the file. IE中的安全功能似乎是不下载文件。 From EricLaw's IEInternals: 来自EricLaw的IEInternals:

if a user tries to download* a file over a HTTPS connection, any response headers that prevent caching will cause the file download process to fail. 如果用户尝试通过HTTPS连接下载*文件,则任何阻止缓存的响应头都将导致文件下载过程失败。

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

Nicolas is correct that "octetstream" (without the dash) is not a known MIME Type . Nicolas是正确的,“octetstream”(没有破折号)不是已知的MIME类型

I suggest using application/pdf . 我建议使用application/pdf

Does it make a difference if you use response.TransmitFile / response.WriteFile ? 如果你使用response.TransmitFile / response.WriteFile会有所作为吗?

TransmitFile (MSDN) TransmitFile(MSDN)
WriteFile(MSDN) WriteFile的(MSDN)

OK, I've corrected the content type to application/octet-stream and changed the caching. 好的,我已将内容类型更正为application / octet-stream并更改了缓存。 It seems to be an IE + SSL issue, so I'll find out if it works when it's deployed later this evening. 它似乎是一个IE + SSL问题,因此我会在今晚晚些时候部署它时发现它是否有效。 Thanks for the help. 谢谢您的帮助。

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