Use Adodb. Stream to directly download files

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.