Original work: possible_Y, downloaded from Times Classroom.
Directly enter the URL path of a doc, xls, or jpg file in the address bar of the browser, and the file will be displayed directly in the browser. In many cases, we hope to directly pop up a download prompt box for users to download. What should we do? There are two methods here:
1. Set up your server's iis and map suffixes such as doc
2. Set its contenttype when sending to the client.
Method 2 is explained in detail below
<%
Response.Buffer = true
Response.Clear
dim url
Dim fso,fl,flsize
dimDname
Dim objStream,ContentType,flName,isre,url1
'****************************************** is passed in when calling download file name
Dname=trim(request("n"))
'************************************************ *****************
If Dname<>"" Then
'******************************Server directory where downloaded files are stored
url=server.MapPath("/")&""&Dname
'************************************************ **
End If
Set fso=Server.CreateObject("Scripting.FileSystemObject")
Set fl=fso.getfile(url)
flsize=fl.size
flName=fl.name
Set fl=Nothing
Set fso=Nothing
%>
<%
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1
objStream.LoadFromFile url
Select Case lcase(Right(flName, 4))
Case ".asf"
ContentType = "video/x-ms-asf"
Case ".avi"
ContentType = "video/avi"
Case ".doc"
ContentType = "application/msword"
Case ".zip"
ContentType = "application/zip"
Case ".xls"
ContentType = "application/vnd.ms-excel"
Case ".gif"
ContentType = "image/gif"
Case ".jpg", "jpeg"
ContentType = "image/jpeg"
Case ".wav"
ContentType = "audio/wav"
Case ".mp3"
ContentType = "audio/mpeg3"
Case ".mpg", "mpeg"
ContentType = "video/mpeg"
Case ".rtf"
ContentType = "application/rtf"
Case ".htm", "html"
ContentType = "text/html"
Case ".txt"
ContentType = "text/plain"
Case Else
ContentType = "application/octet-stream"
End Select
Response.AddHeader "Content-Disposition", "attachment; filename=" & flName
Response.AddHeader "Content-Length", flsize
Response.Charset = "UTF-8"
Response.ContentType = ContentType
Response.BinaryWrite objStream.Read
Response.Flush
response.Clear()
objStream.Close
Set objStream = Nothing
%>
Save the following into download.asp and then you can use <a herf=" http://www.downcodes.com/download.asp?n=file.doc">download!</ a >To download file.doc in the same directory!
But there is a problem here that it is unsafe to write the file.doc path directly in the URL, so the solution should be to save the file.doc path in the database. After searching the database, you can get the path.
If you add a judgment at the front of this program:
if instr(Request.ServerVariables("HTTP_REFERER"),"http://your domain name")=0 then
Response.End
end if
can very well prevent others from stealing links.