asp下载文件代码
asp下载文件的代码,目的让用户点击时直接显示下载。当前有些虚拟空间主机支持asp,但是不支持下载文件,可以使用下面的代码达到下载的效果。
把下面的代码保存为 download.asp,下载的文件名以 filename为参数。
- <%
- 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> 文件不存在<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
- set filename =request.querystring("filename")
- downloadFile("download/" & filename)
- //downloadFile("download/sms.rar")
- %>
欢迎转载,请注明出处:亲亲宝宝