最近做的一個asp項目需要強制下載圖片,我在網上找了很多關于下載遠程圖片的ASP代碼,但測試結果都不行。沒辦法只好自己操刀了,有什么紕漏的地方還請大家指出。
圖片不進行處理,圖片默認是用瀏覽器打開顯示,如果希望圖片變為下載可以使用以下代碼
- function downloadFile(strFile)
- strFilename = server.MapPath(strFile)
- Response.Buffer = True
- Response.Clear
- Set s = Server.CreateObject("ADODB.Stream")
- s.Open
- s.Type = 1
- on error resume next
- Set fso = Server.CreateObject("Scripting.FileSystemObject")
- if not fso.FileExists(strFilename) then
- Response.Write("<h1>Error:</h1>" & strFilename & " does not exist<p>")
- Response.End
- end if
- Set f = fso.GetFile(strFilename)
- intFilelength = f.size
- s.LoadFromFile(strFilename)
- if err then
- Response.Write("<h1>Error: </h1>" & err.Description & "<p>")
- Response.End
- end if
- Response.AddHeader "Content-Disposition", "attachment; filename=" & f.name
- Response.AddHeader "Content-Length", intFilelength
- Response.CharSet = "UTF-8"
- Response.ContentType = "application/octet-stream"
- Response.BinaryWrite s.Read
- Response.Flush
- s.Close
- Set s = Nothing
- End Function
以上所述就是本文給大家分享的函數了,希望對大家學習asp能夠有所幫助。
新聞熱點
疑難解答