我正在尝试使用存储在 MSSQL varbinary(MAX) 字段中的 PDF 回复客户端。该响应在我的本地主机和通过 http 连接的测试服务器上工作,但在通过 https 连接的生产服务器上不起作用。我只使用一个简单的 BinaryWrite (代码如下)。
byte[] displayFile = DatabaseFiles.getPdfById(id);
Response.ContentType = "application/pdf";
Response.BinaryWrite(displayFile);
这里没什么特别的。只需获取二进制数据,设置内容类型,然后写回客户端即可。为了以这种方式通过 https 进行响应,是否需要做一些特殊的事情?
Edit:通过不起作用,我的意思是我在浏览器中看到一个空白文档。 Acrobat 无法在浏览器中加载。
Edit:我刚刚注意到这个问题只发生在 IE 7 中。PDF 在 Firefox 3 中正确加载。我们的客户专门使用 IE 7(比 IE 6 更好,我说服他们升级...哈哈)。
Edit:尝试添加标题“内容处置”以使文件充当附件。浏览器无法在 SSL 下加载,并出现 IE 错误“Internet Explorer 无法从 ProductionServer.net 下载 displayFile.aspx”。 (代码如下)
byte[] displayFile = DatabaseFiles.getPdfById(id);
Response.Clear();
Response.AddHeader("content-disposition", String.Format("attachment;filename={0}", fileName));
Response.ContentType = "application/pdf";
Response.BinaryWrite(displayFile);
Edit:如果在生产服务器上通过 http 查看文件,浏览器将显示 PDF 的代码,就像通过记事本查看一样。 (例如 %PDF-1.4 %âãÏÓ 6 0 obj endobj xref 6 33 ...等)
我刚刚设法通过替换来解决这个问题
Response.Clear();
with
Response.ClearContent();
Response.ClearHeaders();
所以整个事情看起来像:
byte[] downloadBytes = doc.GetData();
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = true;
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Length", downloadBytes.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=myFile.pdf");
Response.BinaryWrite(downloadBytes);
Response.Flush();
Response.End();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)