如何让浏览器直接下载一个文件,而不是打开它,比如word文档.要ASP的解决方案,不要.net

vertears 2005-11-21 02:20:03
如何让浏览器直接下载一个文件,而不是打开它,比如word文档.要ASP的解决方案,不要.net
...全文
449 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
vertears 2005-11-21
  • 打赏
  • 举报
回复
多谢,我自己刚才也找到了另外一种方法:)
一起贴出来吧
----------------------------------------------------------
转自http://dev.csdn.net/develop/article/23/23056.shtm
----------------------------------------------------------
在浏览器的地址栏里直接输入一个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="http://xxx.xxx.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

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



作者Blog:http://blog.csdn.net/possible_Y/
jspadmin 2005-11-21
  • 打赏
  • 举报
回复
呵呵,这是我升到一星用户后的第一帖,楼主幸运
<%
function download(f,n)
'f文件全路径,n下载文件的文件名
on error resume next
Set S=CreateObject("Adodb.Stream")
S.Mode=3
S.Type=1
S.Open
S.LoadFromFile(f)
if Err.Number>0 then
Reaponse.status="404"
else
Response.ContentType="application/octet-stream"
Response.AddHeader "Content-Disposition:","Attachment;filename="&n
Range=Mid(Request.ServerVariables("HTTP_RANGE"),7)
if Range="" then
Response.BinaryWrite(S.Read)
else
S.Postion=Clng(Split(Range,"-")(0))
Response.BinaryWrite(S.Read)
end if
end if
Response.end
end function

dim filename
'filename="index.htm"
filename="1.txt"
call download(server.MapPath(filename),filename)
%>
----------------------------------------------------------------------------------
欢迎光临我的小站(提供免费博客申请):http://www.pifoo.com,有问题的话,可以在留言本上给我留言http://www.pifoo.com/guestbook/


vertears 2005-11-21
  • 打赏
  • 举报
回复
可惜这种方法是没用的,要是碰到安全性高的浏览器,直接拦截弹出窗口
xtuwz 2005-11-21
  • 打赏
  • 举报
回复
response.write"<script>window.open('1.rar');</script>"

28,407

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧