设为首页
收藏本站
最新电影
> 子栏目 Asp | Jsp | XML | XSL | Div+Css | 自定义函数 | 数据库 | 脚本特效
您现在的位置: 首页=>后台技术=>Asp 订阅本栏目  
利用Adodb.Stream直接下载文件
时间: 2007-09-24 23:24:39 阅读次数:1891


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

下面详细说明方法2
程序代码:






以下是代码片段:
_code style="DISPLAY: none" name="html_code"><% 
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 

%> 
_lightcode style="BORDER-RIGHT: #999999 1px dotted; PADDING-RIGHT: 5px; BORDER-TOP: #999999 1px dotted; PADDING-LEFT: 5px; FONT-SIZE: 11px; PADDING-BOTTOM: 5px; BORDER-LEFT: #999999 1px dotted; PADDING-TOP: 5px; BORDER-BOTTOM: #999999 1px dotted; FONT-FAMILY: sans-serif; HEIGHT: 40px; BACKGROUND-COLOR: #f9f9f9" name="html_lightcode"><% 
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 

%> 
_sunny.gif" width=0 onload=show(this)> 

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

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

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

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

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



站内搜索    

下一篇使用XMLHttp和ADODB.Stream取得远程文件并保存到本地

上一篇vbscript Asc 函数 使用介绍

本栏目最新 栏目最新列表
HTTP 401.5 - 未经授权:ISAPI/CGI 应用程序
Asp使用FSO写UTF-8编码文件的方法
asp操作,控制excel的方法
让ewebeditor兼容ie8的方法
Asp实现二级域名的方法
网站优化策划 栏目最新列表
增加网站外链的快速方法
网站上线前必做的30个检查
新的友情链接参考标准(没有google的PR情况
优化Google的AdSense广告的五个工具
王通讲SEO八大基础
站点最新 站点最新列表
微博推广的一些技巧
xhEditor v1.1.7 发布,
收集的一些轻量级非常实
50个新鲜兼容最新版本的
javascript中cookie的设
Excel中出现#VALUE!、#D
jquery插件:飞飞表情插件
十个使用HTML5开发的精彩
支持HTML5的浏览器有哪些
飞妮莫属:漫画:如何写出
历史最热10条信息  
MIME介绍 及 [1] [2] [3]
巧用Google和迅雷来下载
Transact SQL 常 [1] [2]
VIA Rhine II Fast Ethe
电脑常用端 [1] [2] [3]
Do you get a kick out
十道羊皮卷 欣赏+mp3版+
每日一句:A friend and
每日一句:Theres no tu
经典__悟透JavaScript
 width= 
伟哥博客 西安房产 123最新电影 三四六四