我有一个页面,当用户单击按钮时,会动态生成 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 无法从 thesite.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
最佳答案
最近我遇到了同样的错误。就我而言,我使用的是 https 而没有缓存。不下载文件似乎是 IE 中的一项安全功能。来自 EricLaw 的 IEInternals:
http://blogs.msdn.com/b/ieinternals/archive/2009/10/02/internet-explorer-cannot-download-over-https-when-no-cache.aspx
关于c# - 从aspx页面下载PDF,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4163203/