[ASP][Object] Download File by ADODB Stream

檔案下載 by Stream

許多例子,都是使用 Stream 物件來當作下載用途,目的是隱藏下檔檔案來源。這個方法在 Windows 2003 IIS6 中對 asp 的限制,只能作 4MB 大小檔案傳輸,對 aspx 則似乎沒有限制。

可以找到伺服器中的這個檔案
\Windows\System32\Inetsrv\Metabase.xml,把 AspBufferingLimit 值改掉即可。


filename=Server.MapPath("images/"&Request.QueryString("f"))
Set fso = Server.CreateObject("Scripting.FileSystemObject")

'簡查檔案是否存在
If fso.FileExists(filename) Then

'取得檔案資料
Set objFile = fso.GetFile(filename) 
Response.Clear

'使用者接收設定
Response.AddHeader "Content-Disposition", "attachment; filename=" & objFile.Name
Response.AddHeader "Content-Length", objFile.Size
Response.ContentType = "application/octet-stream"

'建立 ADODB Stream 物件
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open

'二進位方式傳送
objStream.Type = 1

'設定編碼
Response.CharSet = "UTF-8"

'載入檔案
objStream.LoadFromFile(filename)

'二進為方式傳送
Response.BinaryWrite(objStream.Read)

'完成,關閉物件
objStream.Close
Set objStream = Nothing
Set objFile = Nothing

Else

Response.Clear
Response.Write("檔案不存在")

End If
Set fso = Nothing