A recent asp project I have done requires forced download of pictures. I have found a lot of ASP codes for downloading remote pictures online, but the test results are not good. I have no choice but to do it myself. Please point out any mistakes.
The picture is not processed. The picture is displayed by default in the browser. If you want the picture to be downloaded, you can use the following code.
- functiondownloadFile(strFile)
- strFilename=server.MapPath(strFile)
- Response.Buffer=True
- Response.Clear
- Sets=Server.CreateObject(ADODB.Stream)
- s.Open
- s.Type=1
- onerrorresumenext
- Setfso=Server.CreateObject(Scripting.FileSystemObject)
- ifnotfso.FileExists(strFilename)then
- Response.Write(<h1>Error:</h1>&strFilename&doesnotexist<p>)
- Response.End
- endif
- Setf=fso.GetFile(strFilename)
- intFilelength=f.size
- s.LoadFromFile(strFilename)
- iferrthen
- Response.Write(<h1>Error:</h1>&err.Description&<p>)
- Response.End
- endif
- Response.AddHeaderContent-Disposition,attachment;filename=&f.name
- Response.AddHeaderContent-Length,intFilelength
- Response.CharSet=UTF-8
- Response.ContentType=application/octet-stream
- Response.BinaryWrites.Read
- Response.Flush
- s.Close
- Sets=Nothing
- EndFunction
The above is the function shared by this article. I hope it will be helpful to everyone to learn Asp.