【发布时间】:2014-03-28 00:12:16
【问题描述】:
我有一个烦人的问题: 我应该在浏览器中显示 PDF(内联显示,而不是下载)。
到目前为止,使用下面的代码,它适用于 Internet Explorer。 但在 google-chrome 中,它只是下载。
在同一台服务器上,执行相同操作的第 3 方应用程序可以正常工作。
我想问题是您在内容类型标题中看到的“应用程序/八位字节流”......
我觉得这很烦人。
我的代码设置了内容类型 application/pdf,当我查看发送的实际标头时,我看到它是 application/octet-stream...
这是因为 mime 是 octet-stream 而不是 application/pdf...
我只有一个问题:为什么?为什么 ?为什么 ? (为什么它设置八位字节流,而不是代码中设置的 application/pdf - 请参阅下面的完整代码)
奖励问题:如果我将 Content-Length 设置为字节数组的长度,为什么传输编码会被分块?
有趣的是,它在我的本地开发服务器上运行良好,所以这似乎与 IIS >= 7 的弊端有关...
ashx:
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim baPDF As Byte() = GetPdfFromImage(Me.Data)
'context.Response.Write(COR.Tools.JSON.JsonHelper.Serialize(Me.Data(context)))
context.Response.Clear()
'context.Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName)
context.Response.AddHeader("Content-Disposition", Portal.ASP.NET.GetContentDisposition("Drucken.pdf", "inline"))
context.Response.AddHeader("Content-Length", baPDF.Length.ToString())
' context.Response.ContentType = "application/msword"
' context.Response.ContentType = "application/octet-stream"
' https://superuser.com/questions/219870/how-to-open-pdf-in-chromes-integrated-viewer-without-downloading-it#
' context.Response.ContentType = "text/html"
context.Response.ContentType = "application/pdf"
context.Response.BinaryWrite(baPDF)
context.Response.Flush()
context.Response.End()
End Sub
' COR.ASP.NET.StripInvalidPathChars("") '
Public Shared Function StripInvalidPathChars(str As String) As String
Dim strReturnValue As String = Nothing
If str Is Nothing Then
Return strReturnValue
End If
Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
Dim achrInvalidPathChars As Char() = System.IO.Path.GetInvalidPathChars()
For Each cThisChar As Char In str
Dim bIsValid As Boolean = True
For Each cInvalid As Char In achrInvalidPathChars
If cThisChar = cInvalid Then
bIsValid = False
Exit For
End If
Next cInvalid
If bIsValid Then
sb.Append(cThisChar)
End If
Next cThisChar
strReturnValue = sb.ToString()
sb = Nothing
Return strReturnValue
End Function ' StripInvalidPathChars '
Public Shared Function GetContentDisposition(ByVal strFileName As String) As String
Return GetContentDisposition(strFileName, "attachment")
End Function ' GetContentDisposition '
' http://www.iana.org/assignments/cont-disp/cont-disp.xhtml '
Public Shared Function GetContentDisposition(ByVal strFileName As String, ByVal strDisposition As String) As String
' http://stackoverflow.com/questions/93551/how-to-encode-the-filename-parameter-of-content-disposition-header-in-http '
Dim contentDisposition As String
strFileName = StripInvalidPathChars(strFileName)
If String.IsNullOrEmpty(strDisposition) Then
strDisposition = "inline"
End If
If System.Web.HttpContext.Current IsNot Nothing AndAlso System.Web.HttpContext.Current.Request.Browser IsNot Nothing Then
If (System.Web.HttpContext.Current.Request.Browser.Browser = "IE" And (System.Web.HttpContext.Current.Request.Browser.Version = "7.0" Or System.Web.HttpContext.Current.Request.Browser.Version = "8.0")) Then
contentDisposition = strDisposition + "; filename=" + Uri.EscapeDataString(strFileName).Replace("'", Uri.HexEscape("'"c))
ElseIf (System.Web.HttpContext.Current.Request.Browser.Browser = "Safari") Then
contentDisposition = strDisposition + "; filename=" + strFileName
Else
contentDisposition = strDisposition + "; filename*=UTF-8''" + Uri.EscapeDataString(strFileName)
End If
Else
contentDisposition = strDisposition + "; filename*=UTF-8''" + Uri.EscapeDataString(strFileName)
End If
Return contentDisposition
End Function ' GetContentDisposition '
这是第 3 方应用程序的标题,Chrome 可以正常显示
【问题讨论】:
-
您是否真的在服务器上设置了正确的 MIME?
-
您是否查看过其他浏览器中的标题 - 它们是否显示应用程序/pdf?
-
您是否查看过本文中描述的问题? codeproject.com/Articles/671592/…
标签: c# asp.net .net vb.net google-chrome