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