Enter the URL path of a doc, xls, or JPG file in the address bar of the browser. The file will be directly displayed in the browser. In many cases, we hope to bring up a download prompt box for users to download. What should we do? There are two methods:
1. Set IIS on your server and map extension names such as Doc.
2. Set its contenttype when sending messages to the client
Method 2 is described in detail below
Program code: <%
Response. Buffer = true
Response. Clear
Dim URL
Dim FSO, FL, flsize
Dim dname
Dim objstream, contenttype, flname, isre, url1
'*************************************** * ***** The downloaded file name passed in during the call
Dname = trim (Request ("N "))
'*************************************** ***************************
If dname <> "then
* Download the server directory where the file is 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
%>
<%
Response. Buffer = true
Response. Clear
Dim URL
Dim FSO, FL, flsize
Dim dname
Dim objstream, contenttype, flname, isre, url1
'*************************************** * ***** The downloaded file name passed in during the call
Dname = trim (Request ("N "))
'*************************************** ***************************
If dname <> "then
* Download the server directory where the file is 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 above content as download. asp and then you can use <a herf = "http: // xxx.xxx.com/download.asp? N=file.doc "> download! </A> download the file.doc file in the same directory!
However, it is insecure to directly write file.dockers in url. therefore, the solution should be to save file.doc paths to the database, and get the path after searching for the database.
Add a judgment at the beginning of the program:
If instr (request. servervariables ("http_referer"), "http: // your domain name") = 0 then
Response. End
End if
It can effectively prevent leeching by others.