下面的ASP代码抓取文件的内容(thisoutfile - 其GUID作为其文件名)并将其流式传输到浏览器,提供要保存的建议文件名。这适用于除chrome之外的所有浏览器,其中提供的文件名是脚本本身的名称,即使文件名(thisfname)是单个单词也是如此。如果您实际将文件保存为xls或pdf,则将其保存为OK。
知道为什么吗?
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Set f=fs.GetFile(thisoutfile)
Set adoStream = CreateObject("ADODB.Stream")
adoStream.Open()
adoStream.Type = 1
adoStream.LoadFromFile(f.Path)
dataSize = f.size
Response.Buffer = true
Response.CharSet = "UTF-8"
Response.clear
select case ext
case "xls"
response.contentType="application/vnd.ms-excel"
case else
Response.ContentType = "application/x-unknown" ' arbitrary
end select
Response.AddHeader "Content-Length", dataSize
Response.AddHeader "Content-Disposition","attachment;filename=""" & thisfname & ""
Response.flush
do while not adoStream.eos
Response.BinaryWrite adoStream.Read(1024 * 8)
Response.flush
loop
Response.End()
adoStream.close
set adoStream=nothing
set f=nothing
set fs=nothing
答案 0 :(得分:2)
Content-Disposition标题中缺少双引号,而Chrome似乎并不宽容。将该行代码更改为
Response.AddHeader "Content-Disposition","attachment;filename=""" & thisfname & """"