2011-06-01 76 views
1

下面的ASP代碼抓取了一個文件(thisoutfile--其中包含一個GUID作爲文件名)的內容並將其流式傳輸到瀏覽器,並提供建議的文件名以保存爲。這適用於除chrome外的所有瀏覽器,即使文件名(thisfname)是單個字,文件名所提供的也是腳本本身的名稱。如果你真的將文件保存爲xls或pdf,那麼它可以保存它。Chrome不遵守ado.stream文件名

任何想法爲什麼?

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 

回答

2

Content-Disposition標題中缺少雙引號,看起來Chrome並非那麼寬容。將該行代碼更改爲

Response.AddHeader "Content-Disposition","attachment;filename=""" & thisfname & """" 
+0

是的就是這樣 - 謝謝 – derekcohen 2011-06-01 20:18:44