- 利用stream直接下载文件
- 作者:未知 来源:未知 添加时间:2004年1月5日 字体:大 中 小
- 80800在浏览器的地址栏里直接输入一个doc或xls或jpg的文件的url路径,那么该文件会直接显示在浏览器里。而在很多时候我t'x4们希望能直接弹出下载提示框让用户下载,我t'x4们该怎么办呢?这里有两种方法:
t'x41、设置你的服务器的iis,给doc等后缀名做映射
t'x42、在向客户端发送时设置其contenttype
t'x4
t'x4下面详细说明方法2
t'x4
t'x4<%
t'x4Response.Buffer = true
t'x4Response.Clear
t'x4
t'x4dim url
t'x4Dim fso,fl,flsize
t'x4dim Dname
t'x4Dim objStream,ContentType,flName,isre,url1
t'x4'*********************************************调用时传入的下载文件名
t'x4Dname=trim(request("n"))
t'x4'******************************************************************
t'x4If Dname<>"" Then
t'x4'******************************下载文件存放的服务端目录
t'x4url=server.MapPath("/")&"\"&Dname
t'x4'***************************************************
t'x4End If
t'x4
t'x4Set fso=Server.CreateObject("Scripting.FileSystemObject")
t'x4Set fl=fso.getfile(url)
t'x4flsize=fl.size
t'x4flName=fl.name
t'x4Set fl=Nothing
t'x4Set fso=Nothing
t'x4%>
t'x4<%
t'x4Set objStream = Server.CreateObject("ADODB.Stream")
t'x4objStream.Open
t'x4objStream.Type = 1
t'x4objStream.LoadFromFile url
t'x4
t'x4
t'x4Select Case lcase(Right(flName, 4))
t'x4Case ".asf"
t'x4ContentType = "video/x-ms-asf"
t'x4Case ".avi"
t'x4ContentType = "video/avi"
t'x4Case ".doc"
t'x4ContentType = "application/msword"
t'x4Case ".zip"
t'x4ContentType = "application/zip"
t'x4Case ".xls"
t'x4ContentType = "application/vnd.ms-excel"
t'x4Case ".gif"
t'x4ContentType = "image/gif"
t'x4Case ".jpg", "jpeg"
t'x4ContentType = "image/jpeg"
t'x4Case ".wav"
t'x4ContentType = "audio/wav"
t'x4Case ".mp3"
t'x4ContentType = "audio/mpeg3"
t'x4Case ".mpg", "mpeg"
t'x4ContentType = "video/mpeg"
t'x4Case ".rtf"
t'x4ContentType = "application/rtf"
t'x4Case ".htm", "html"
t'x4ContentType = "text/html"
t'x4Case ".txt"
t'x4ContentType = "text/plain"
t'x4Case Else
t'x4ContentType = "application/octet-stream"
t'x4End Select
t'x4
t'x4
t'x4
t'x4Response.AddHeader "Content-Disposition", "attachment; filename=" & flName
t'x4Response.AddHeader "Content-Length", flsize
t'x4
t'x4Response.Charset = "UTF-8"
t'x4Response.ContentType = ContentType
t'x4
t'x4Response.BinaryWrite objStream.Read
t'x4Response.Flush
t'x4response.Clear()
t'x4objStream.Close
t'x4Set objStream = Nothing
t'x4
t'x4%>
t'x4
t'x4将下面的东西存成download.asp然后你就可以用<a herf="http://xxx.xxx.com/download.asp?n=file.doc">download!</a>来下载同一目录下的file.doc了!
t'x4
t'x4但是这里有个问题就是直接将file.doc路径写在url里是不安全的,所以解决方案应该是将file.doc的路径存到数据库里,同过查找数据库后得到路径
t'x4
t'x4在这个程序的最前面如果加上一个判断:
t'x4
t'x4if instr(Request.ServerVariables("HTTP_REFERER"),"http://你的域名")=0 then
t'x4Response.End
t'x4end if
t'x4
t'x4就能够很好的防止别人的盗链了
t'x480797
- 返回页面顶端


- 上一篇:原来处女只值80元
- 下一篇:数据分页方法新思路,速度非常快
返回上一页
打印本文
加入收藏
页面最后更新时间:2010年3月10日