使用Adodb.Stream做檔案下載, 優點是可以隱藏檔案真實路徑與檔名。
函數語法如下:
function downloadfile(Getf,n)
on error resume next
Set Ado_S=CreateObject("Adodb.Stream")
Ado_S.Mode=3
Ado_S.Type=1
Ado_S.Open
Ado_S.LoadFromFile(Getf)
if Err.Number>0 then
Response.Status="404"
else
Response.ContentType="application/octet-stream"
Response.AddHeader "Content-Disposition:","attachment; filename=" & n
Range=Mid(Request.ServerVariables("HTTP_RANGE"),7)
if Range="" then
Response.BinaryWrite(Ado_S.Read)
else
Ado_S.position=Clng(Split(Range,"-")(0))
Response.BinaryWrite(Ado_S.Read)
End if
end if
Response.End
end function
使用InStrRev取得檔案副檔名
FileExt1 = Mid(rs("附件"), InStrRev(rs("附件"), ".") + 1)
F_Ext1=UCase(FileExt1)
’--呼叫downloadfile
call downloadfile(Server.MapPath(""&af1&""),""&file_name&"1." & FileExt1 & "")
本網站模組[單位公告]與[檔案下載]皆提供兩個檔案(附件)上傳與下載服務,第一版直接秀出附件檔名與路徑,第一次改版改為隱藏檔名,先使用dload.asp做來源判斷並做檔案真實路徑隱藏,但下載時仍會以真實檔名下載。這次決定將再度增加安全性,讓使用者在下載時直接以公告主旨為下載附件檔名,完全隱藏真實檔案路徑與名稱。
使用Adodb.Stream必須注意緩衝限制,預設值為4MB(419304--1024*1024*4),另一篇相關日誌http://wap.sjps.tpc.edu.tw/blog/View.asp?cid=769
IIS7 windows 2008 server 設定方式:
