我有一个ASP.net应用程序,它返回一个二进制PDF文件(从数据库中存储,以前上传)到客户端。如何在IE6中显示PDF附件
我的代码对所有浏览器都很好,除了Internet Explorer 6(我的生活故事!)。在IE6中,当用户点击打开时,Adobe报告错误:“打开此文档时出错,无法找到该文件。”
当用户点击保存时,PDF保存正常,可以通过双击打开。我很难过。 Google已经给出了缓存建议(将cachecontrol设置为私有等),并且我尝试了所有这些,但都没有成功。
这个奇怪的行为是,当我从头开始在ASP.net服务器层(使用NFop)生成PDF时,IE会打开它(使用相同的代码!)。
这里是我的整个网络发送的二进制数据代码:
// Firefox doesn't like spaces in filenames.
filename = filename.Replace(" ", "_");
string extension = Path.GetExtension(filename);
string contentType;
switch (extension)
{
case "pdf":
contentType = "application/pdf";
break;
default:
contentType = "application/x-unknown";
break;
}
context.Response.Clear();
context.Response.AddHeader("content-disposition", "attachment;filename=" + filename);
context.Response.Charset = "";
context.Response.ContentType = contentType;
context.Response.BinaryWrite(data);
context.Response.Flush();
这里是应用程序版本:
- ASP .NET 3.5
- IE6.0.2900.2180.xpsp_s
- 列表项目
- p2_gdr.091208-2028
- Adobe Reader版本8.0.0
- 的Windows XP SP 2
- SQL Server 2008中
任何帮助/建议将不胜感激。谢谢:)
编辑:
我已经插上提琴手查看头果然它似乎是一个缓存的问题。哎呀!
当我的NFop PDF(工作的)被上传时,它正在发送cache-control = private。 当我的附件PDF(不工作的)被上传时,它发送no-cache。
我看过Response对象,当我调用context.Response.Flush()时,它们看起来都有相同的头文件。
仍然难倒!
解决:
某处在我们的框架是将其正在使用反射调用的流氓方法:
/// ///设置请求的expiratoin并迫使没有缓存 /// 保护无效SetCacheExpiration(HttpContext上下文) { //设置缓存立即过期 context.Response.Cache.SetCacheability(HttpCacheability.NoCache); context.Response.Cache.SetSlidingExpiration(true); context.Response.Cache.SetExpires(DateTime.Now); context.Response.Cache。SetMaxAge(新的TimeSpan(0,0,0)); context.Response.Cache.SetNoStore(); context.Response.Cache.SetAllowResponseInBrowserHistory(false); context.Response.Cache.SetValidUntilExpires(false); context.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
}
感谢您的帮助,缓存!有趣的是,实际上没有缓存下载(打开时)的唯一浏览器是IE6。
你应该调用'Path.GetExtension(fileName)'。 – SLaks 2010-02-23 22:42:53
感谢您的提示! :) – Russell 2010-02-23 22:44:54
请注意,'Path.GetExtension(fileName)'将包含'.'。 – SLaks 2010-02-23 22:58:01