此代码实现不管文件是什么格式(如:jpg)在浏览器打开都会提示下载.
方法很简单:
1.将下面的代码保存为:download.asp
2.文件调用方法:http://www.XXX.com/down.asp?FileName=aaa.jpg

<%
‘━━━━━━━━━━━━━━━━━━━━━━━━━━
‘【文件名】VC_Download.asp
‘【功 能】实现任何后缀名的文件都出现下载
‘【修 改】iFu25.CN
‘【日 期】2008年4月29日
‘━━━━━━━━━━━━━━━━━━━━━━━━━━
Response.Buffer = true
Response.Clear

dim url
Dim fso,fl,flsize
dim Dname
Dim objStream,ContentType,flName,isre,url1
‘*********************************************调用时传入的下载文件名
Dname=trim(request(“FileName”))
‘******************************************************************
If Dname<>“” Then
‘******************************下载文件存放的服务端目录
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

%>

但是这里有个问题就是直接将aaa.jpg路径写在url里是不安全的,所以解决方案应该是将file.doc的路径存到数据库里,同过查找数据库后得到路径.

在这个程序的最前面如果加上一个判断就能够很好的防止别人的盗链了:

if instr(Request.ServerVariables(“HTTP_REFERER”),”http://你的域名”)=0 then
Response.End
end if

发表评论

/ 快捷键:Ctrl+Enter
加载中……