加载中...
查看文章
  • 利用stream直接下载文件
  • 作者:未知 来源:未知 添加时间:2004年1月5日 字体:
  • 83717在浏览器的地址栏里直接输入一个doc或xls或jpg的文件的url路径,那么该文件会直接显示在浏览器里。而在很多时候我6M5@Z们希望能直接弹出下载提示框让用户下载,我6M5@Z们该怎么办呢?这里有两种方法:

    6M5@Z1、设置你的服务器的iis,给doc等后缀名做映射

    6M5@Z2、在向客户端发送时设置其contenttype

    6M5@Z

    6M5@Z下面详细说明方法2

    6M5@Z

    6M5@Z<%

    6M5@ZResponse.Buffer = true

    6M5@ZResponse.Clear

    6M5@Z

    6M5@Zdim url

    6M5@ZDim fso,fl,flsize

    6M5@Zdim Dname

    6M5@ZDim objStream,ContentType,flName,isre,url1

    6M5@Z'*********************************************调用时传入的下载文件名

    6M5@ZDname=trim(request("n"))

    6M5@Z'******************************************************************

    6M5@ZIf Dname<>"" Then

    6M5@Z'******************************下载文件存放的服务端目录

    6M5@Zurl=server.MapPath("/")&"\"&Dname

    6M5@Z'***************************************************

    6M5@ZEnd If

    6M5@Z

    6M5@ZSet fso=Server.CreateObject("Scripting.FileSystemObject")

    6M5@ZSet fl=fso.getfile(url)

    6M5@Zflsize=fl.size

    6M5@ZflName=fl.name

    6M5@ZSet fl=Nothing

    6M5@ZSet fso=Nothing

    6M5@Z%>

    6M5@Z<%

    6M5@ZSet objStream = Server.CreateObject("ADODB.Stream")

    6M5@ZobjStream.Open

    6M5@ZobjStream.Type = 1

    6M5@ZobjStream.LoadFromFile url

    6M5@Z

    6M5@Z

    6M5@ZSelect Case lcase(Right(flName, 4))

    6M5@ZCase ".asf"

    6M5@ZContentType = "video/x-ms-asf"

    6M5@ZCase ".avi"

    6M5@ZContentType = "video/avi"

    6M5@ZCase ".doc"

    6M5@ZContentType = "application/msword"

    6M5@ZCase ".zip"

    6M5@ZContentType = "application/zip"

    6M5@ZCase ".xls"

    6M5@ZContentType = "application/vnd.ms-excel"

    6M5@ZCase ".gif"

    6M5@ZContentType = "image/gif"

    6M5@ZCase ".jpg", "jpeg"

    6M5@ZContentType = "image/jpeg"

    6M5@ZCase ".wav"

    6M5@ZContentType = "audio/wav"

    6M5@ZCase ".mp3"

    6M5@ZContentType = "audio/mpeg3"

    6M5@ZCase ".mpg", "mpeg"

    6M5@ZContentType = "video/mpeg"

    6M5@ZCase ".rtf"

    6M5@ZContentType = "application/rtf"

    6M5@ZCase ".htm", "html"

    6M5@ZContentType = "text/html"

    6M5@ZCase ".txt"

    6M5@ZContentType = "text/plain"

    6M5@ZCase Else

    6M5@ZContentType = "application/octet-stream"

    6M5@ZEnd Select

    6M5@Z

    6M5@Z

    6M5@Z

    6M5@ZResponse.AddHeader "Content-Disposition", "attachment; filename=" & flName

    6M5@ZResponse.AddHeader "Content-Length", flsize

    6M5@Z

    6M5@ZResponse.Charset = "UTF-8"

    6M5@ZResponse.ContentType = ContentType

    6M5@Z

    6M5@ZResponse.BinaryWrite objStream.Read

    6M5@ZResponse.Flush

    6M5@Zresponse.Clear()

    6M5@ZobjStream.Close

    6M5@ZSet objStream = Nothing

    6M5@Z

    6M5@Z%>

    6M5@Z

    6M5@Z将下面的东西存成download.asp然后你就可以用<a herf="http://xxx.xxx.com/download.asp?n=file.doc";>download!</a>来下载同一目录下的file.doc了!

    6M5@Z

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

    6M5@Z

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

    6M5@Z

    6M5@Zif instr(Request.ServerVariables("HTTP_REFERER"),"http://你的域名")=0 then

    6M5@ZResponse.End

    6M5@Zend if

    6M5@Z

    6M5@Z就能够很好的防止别人的盗链了

    6M5@Z83714
  • 返回页面顶端
  • 上一篇:原来处女只值80元
  • 下一篇:数据分页方法新思路,速度非常快
  • 返回上一页 打印本文 加入收藏
  • 页面最后更新时间:2011年5月18日