您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> asp编程 >> 正文

ASP利用adodb.stream下载文件但不打开的方法

作者:佚名    责任编辑:admin    更新时间:2022-06-22

 在浏览器的地址栏里直接输入一个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" 'liehuo.net
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()