asp中使用流方式下载文件的代码

将下面的内容存成download.asp 
然后你就可以用<a  herf="http://xxx.xxx.com/download.asp?n=file.doc">download!</a> 
来下载了! 
------------------------------------------------------------ 
<% 
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 
 
%> 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值