哪种 ContentType 用于流式文件以在浏览器中查看?
Which ContentType to use for streaming file to view in browser?
我正在尝试使用 adodb 将文件流式传输到浏览器。
用户得到这样的选项:
Save File
选项按预期工作。
但是 Open with Firefox
选项给出了 gobbledy,并且 .htm 被附加到文件名的末尾。
我推测这与 Response.ContentType
的设置有关。
我试过使用 application/pdf
、application/octet-stream
和 application/x-unknown
。但无论我使用哪个,我总是得到相同的结果。
这是怎么回事?我应该使用什么 ContentType?
使用 Firefox 打开的截图
ADODB 流代码
Dim objStream
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type = 1
objStream.Open
objStream.LoadFromFile(fileDirectory & "\" & fileToDownload)
Response.AddHeader "Expires", "0"
Response.AddHeader "Content-Transfer-Encoding", "binary"
Response.AddHeader "Content-Description", "File Transfer"
Response.AddHeader "Content-Disposition", "attachment; filename=" & fileToDownload
Response.AddHeader "Content-Length", objStream.Size
Response.CharSet = "UTF-8"
Response.ContentType = "???" // *** what should I put here? ***
Response.BinaryWrite objStream.Read
objStream.Close
Set objStream = Nothing
除了Response.ContentType = "application/pdf"
,我还需要Response.AddHeader "Content-Type", "application/pdf"
。
所以最终有效的代码是:
Dim objStream
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type = 1 'adTypeBinary
objStream.Open
objStream.LoadFromFile(fileDirectory & "\" & fileToDownload)
Response.AddHeader "Expires", "0"
Response.AddHeader "Content-Transfer-Encoding", "binary"
Response.AddHeader "Content-Description", "File Transfer"
Response.AddHeader "Content-Disposition", "attachment; filename=" & fileToDownload
Response.AddHeader "Content-Length", objStream.Size
Response.AddHeader "Content-Type", "application/pdf" '** added this line **
Response.CharSet = "UTF-8"
Response.ContentType = "application/pdf"
Response.BinaryWrite objStream.Read
objStream.Close
Set objStream = Nothing
我正在尝试使用 adodb 将文件流式传输到浏览器。
用户得到这样的选项:
Save File
选项按预期工作。
但是 Open with Firefox
选项给出了 gobbledy,并且 .htm 被附加到文件名的末尾。
我推测这与 Response.ContentType
的设置有关。
我试过使用 application/pdf
、application/octet-stream
和 application/x-unknown
。但无论我使用哪个,我总是得到相同的结果。
这是怎么回事?我应该使用什么 ContentType?
使用 Firefox 打开的截图
ADODB 流代码
Dim objStream
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type = 1
objStream.Open
objStream.LoadFromFile(fileDirectory & "\" & fileToDownload)
Response.AddHeader "Expires", "0"
Response.AddHeader "Content-Transfer-Encoding", "binary"
Response.AddHeader "Content-Description", "File Transfer"
Response.AddHeader "Content-Disposition", "attachment; filename=" & fileToDownload
Response.AddHeader "Content-Length", objStream.Size
Response.CharSet = "UTF-8"
Response.ContentType = "???" // *** what should I put here? ***
Response.BinaryWrite objStream.Read
objStream.Close
Set objStream = Nothing
除了Response.ContentType = "application/pdf"
,我还需要Response.AddHeader "Content-Type", "application/pdf"
。
所以最终有效的代码是:
Dim objStream
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type = 1 'adTypeBinary
objStream.Open
objStream.LoadFromFile(fileDirectory & "\" & fileToDownload)
Response.AddHeader "Expires", "0"
Response.AddHeader "Content-Transfer-Encoding", "binary"
Response.AddHeader "Content-Description", "File Transfer"
Response.AddHeader "Content-Disposition", "attachment; filename=" & fileToDownload
Response.AddHeader "Content-Length", objStream.Size
Response.AddHeader "Content-Type", "application/pdf" '** added this line **
Response.CharSet = "UTF-8"
Response.ContentType = "application/pdf"
Response.BinaryWrite objStream.Read
objStream.Close
Set objStream = Nothing