从IIS7.5提供的PDF仅在Internet Explorer中打开

时间:2012-06-12 19:46:41

标签: .net windows pdf iis-7.5 itextsharp

我正在尝试确定这是IIS问题还是代码问题。我倾向于前者,但在那种情况下我不确定如何解决它。

我有几个Asp.Net Web应用程序将PDF创建为MemoryStream(使用iTextSharp),然后使用MemoryStream.ToArray方法在屏幕上显示PDF。以下是我使用的基本代码。

Try 
    Dim m As System.IO.MemoryStream = pdfhelper.createFileMethod(filename)
    Dim data As Byte() = m.ToArray  
    Response.Clear()
    Response.Buffer = True
    Response.AddHeader("Content-Length", data.Length.ToString())
    Response.AddHeader("Content-Disposition", "inline; filename=sample")
    Response.AddHeader("Expires", "0")
    Response.AddHeader("Pragma", "cache")
    Response.AddHeader("Cache-Control", "private")
    Response.ContentType = "application/pdf"
    Response.AddHeader("Accept-Ranges", "bytes")
    Response.BinaryWrite(data)
    Response.Flush()
    Response.End()
Catch ex As Exception
    throw ex
End Try

我从Windows Server 2008 R2标准版64位版本6.1 Build 7601 Service Pack 1(IIS 7.5.7600.16385)提供服务。

当我在Internet Explorer中使用该应用程序时,我可以轻松打开PDF文档。当我使用Chrome或Firefox时,PDF只会在加载时挂起。它根本不会加载。我没有收到错误消息。无论我是在服务器上还是在客户端上运行,问题都是一样的。

但是,当我使用Visual Studio内置的IIS运行我的开发盒时,无论浏览器如何,我都没有问题。

我知道this hotfix.中解决的问题我因此认为问题出在IIS中。但是,我无法应用此修补程序,因为它仅对我的Windows版本的Build 7600有效。

所以回想起来 - 这是IIS吗?这是我的代码吗?只是IE更宽容我正在制作的错误?我该如何解决?

感谢您提出的任何想法。

编辑:我找到了this suggestion,它强制下载PDF而不是在浏览器中打开。绝对不是解决这个问题的理想方案。希望有人有另一个想法。

1 个答案:

答案 0 :(得分:0)

我终于解决了这个问题,虽然我没有100%“为什么”。

1)我将我的代码更改为以下内容,删除等待缓冲区然后结束响应的命令。

    Response.ClearContent()
    Response.ClearHeaders()
    Response.ContentType = "application/pdf" 
    Response.AppendHeader("Content-Length", data.Length.ToString())
    Response.AppendHeader("Content-Disposition", "inline; filename=" + "Sample.pdf")
    Response.AppendHeader("Accept-Ranges", "bytes")
    Response.BinaryWrite(data)

2)更新了这是后面代码的aspx页面。它仍然拥有所有html“东西”。除了页面指令命令之外,我删除了页面上的所有内容。

此时,PDF继续与IE一起使用。在一些人的计算机上使用较旧的(9.x)Adobe插件,Firefox在浏览器中查看没有任何困难。在具有Adobe 10.1.3的计算机上,Firefox会提示“打开或保存”对话框。但Chrome(版本19.0.1084.56)仍然悬而未决。

3)在使用Safari测试并且没有问题后,我认为这是一个Chrome问题。我禁用了Chrome Adob​​e插件,现在PDF按预期显示在浏览器中。