当前位置: 首页 > 图文教程 > 网络编程 > ASP > 利用adodb.stream直接下载任何后缀的文件(防盗链)

ASP
[原创]asp下用实现模板加载的的几种方法总结
asp下利用fso实现文件夹或文件移动改名等操作函数
asp cint clng的范围与防止cint和clng的溢出解决方法大全
asp防范跨站点脚本攻击的的方法
独孤剑写的马克斯迅雷片库采集插件1.4 官方最新版提供下载了
根据日期得到生肖的ASP函数
不错的主要用于加密的vbs(asp)位移运算类
Script.VBS.Agent.ai juan.vbs专杀
循环取值Request.QueryString的用法
asp下实现字符串的补充fill()
无服务器做任意二级米子站
几种另类的ASP后门
asp事物功能简单小应用
ASP获取网页全部图片地址并保存为数组的正则
通过客户端验证上传图片文件大小的ASP源码
asp下计算分页的几种方法
谈动态多行多列,循环行和列,输出M行N列的ASP代码
asp在线执行sql语句的函数
asp正则html的图片,对图自动缩放大小
文件名 正则表达式提取方法

ASP 中的 利用adodb.stream直接下载任何后缀的文件(防盗链)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 199 ::
收藏到网摘: n/a

 

原作:possible_Y,载自时代课堂

在浏览器的地址栏里直接输入一个doc或xls或jpg的文件的url路径,那么该文件会直接显示在浏览器里。而在很多时候我们希望能直接弹出下载提示框让用户下载,我们该怎么办呢?这里有两种方法:
1、设置你的服务器的iis,给doc等后缀名做映射
2、在向客户端发送时设置其contenttype

下面详细说明方法2

<%
Response.Buffer = true
Response.Clear

dim url
Dim fso,fl,flsize
dim Dname
Dim objStream,ContentType,flName,isre,url1
'*********************************************调用时传入的下载文件名
Dname=trim(request("n"))
'******************************************************************
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

%>

将下面的东西存成download.asp然后你就可以用<a herf="download!http://www.knowsky.com/download.asp?n=file.doc">download!</a>来下载同一目录下的file.doc了!

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

在这个程序的最前面如果加上一个判断:

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

就能够很好的防止别人的盗链了