Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从aspx页面下载PDF_C#_Asp.net_Pdf - Fatal编程技术网

C# 从aspx页面下载PDF

C# 从aspx页面下载PDF,c#,asp.net,pdf,C#,Asp.net,Pdf,我有一个页面,当用户点击一个按钮时,会动态生成一个PDF文件供他们下载 这是允许用户下载pdf的代码: // Omitted code that generates the pdf bytes response.ContentType = "application/octetstream"; response.AppendHeader("Content-Disposition", "attachment; filename=" + filename); response.BinaryWrite

我有一个页面,当用户点击一个按钮时,会动态生成一个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无法从网站下载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/octetstream
,而不是在类似问题的解决方案中找到的
application/octetstream

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

尝试使用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”

Nicolas是正确的,因为“octetstream”(不带破折号)不是已知的


我建议使用
application/pdf

最近我遇到了同样的错误。在我的例子中,我使用的是https,没有缓存。不下载文件似乎是IE的一项安全功能。来自EricLaw的IES:

如果用户试图通过HTTPS连接下载*文件,任何阻止缓存的响应头都将导致文件下载过程失败


如果使用
response.TransmitFile
/
response.WriteFile
,会有什么不同吗



好的,我已将内容类型更正为应用程序/八位字节流,并更改了缓存。这似乎是一个IE+SSL问题,所以我将在今晚晚些时候部署它时了解它是否有效。谢谢你的帮助

如果能看到所有响应标题,那就太好了。通过使用
application/pdf
编辑以包含响应标题,大多数浏览器都会直接打开pdf文件,而不是询问用户是否保存该文件。如果配置了插件,application/pdf将允许浏览器显示pdf文件。无论插件配置如何,应用程序/八位字节流都应该请求下载该文件。(就个人而言,我不喜欢在浏览器中打开PDF而不要求保存。)我遇到了同样的问题。这里有一个链接站点的解决方法:“2010年10月更新:我对这个问题进行了一些进一步的调查,发现(令人惊讶的)你可以指定缓存控制:没有存储,没有缓存,下载也可以工作,但是如果你以相反的顺序指定这些指令,它就会失败。”我尝试了这个方法,它成功了。反转缓存控制头。虽然这可能是一个问题,但这并不是导致他特定错误的原因。见乔普的回答。