Download files directly with stream

Source: Internet
Author: User
stream| Download the URL path of a doc or xls or JPG file directly in the browser's address bar, the file will be displayed directly in the browser. And in a lot of times we want to be able to pop the download prompt box to let users download, what should we do? Here are two ways:
1, set up your server IIS, to the doc and other suffix name do mapping
2. Set its contenttype when sending to client

Method 2 is described in detail below

<%
Response.buffer=true
Response.Clear

Dimurl
Dimfso,fl,flsize
Dimdname
Dimobjstream,contenttype,flname,isre,url1
' Download filename passed in when ********************************************* call
Dname=trim (Request ("n"))
'******************************************************************
Ifdname<> "" Then
' ****************************** download files stored in the server directory
Url=server. MapPath ("/") & "&dname"
'***************************************************
EndIf

Setfso=server.createobject ("Scripting.FileSystemObject")
Setfl=fso.getfile (URL)
Flsize=fl.size
Flname=fl.name
Setfl=nothing
Setfso=nothing
%>
<%
Setobjstream=server.createobject ("ADODB.") Stream ")
objStream.Open
Objstream.type=1
Objstream.loadfromfileurl

Selectcaselcase (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"
Caseelse
Contenttype= "Application/octet-stream"
Endselect



Response.AddHeader "Content-disposition", "Attachment;filename=" &flname
Response.AddHeader "Content-length", flsize

response.charset= "UTF-8"
Response.contenttype=contenttype

Response.BinaryWriteobjStream.Read
Response.Flush
Response. Clear ()
objStream.Close
Setobjstream=nothing

%>

Save the following items as download.asp then you can use the <aherf=http://xxx.xxx.com/download.asp?n=file.doc>download!</a> To download the File.doc in the same directory!

But the problem here is that it is not safe to write the File.doc path directly in the URL, so the solution is to save the File.doc path to the database and get the path after the lookup database.

At the very front of the program, if you add a judgment:

Ifinstr (Request.ServerVariables ("Http_referer"), "http://your domain name") =0then
Response.End
endif

will be able to prevent other people's hotlinking well.


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.