ado|stream| anti-theft Chain | Download Original: possible_y, from the time class
In the browser's address bar directly enter a doc or xls or JPG file URL path, then the file will be directly displayed 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
Dim url
Dim fso,fl,flsize
Dim dname
Dim OBJSTREAM,CONTENTTYPE,FLNAME,ISRE,URL1
' Download filename passed in when ********************************************* call
Dname=trim (Request ("n"))
'******************************************************************
If dname<> "" Then
' ****************************** download files stored in the server directory
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 items as download.asp then you can use <a herf= "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:
If InStr (Request.ServerVariables ("Http_referer"), "http://your domain name") =0 Then
Response.End
End If