下载任意类型的文件

来源:互联网 发布:python黑客编程书籍 编辑:程序博客网 时间:2024/05/16 08:27

<%
Response.Buffer = true
Response.Clear 

'获取要下载的文件在服务器上的绝对位置
  dlfile=trim(request("dlfile"))
  If dlfile<>"" Then
  fileurl=server.MapPath(dlfile)
  Else
  Response.end
  End If

'创建Myfso,使用FSO组件
  Set Myfso=Server.CreateObject("Scripting.FileSystemObject")
  Set f=Myfso.getfile(fileurl) '定义FSO对象f
    fsize=f.size '文件大小
    fName=f.name '文件名字
  Set f=Nothing  '释放f
  Set Myfso=Nothing '释放MyFso

'使用Adodb.Stream组件
Set MyStream = Server.CreateObject("ADODB.Stream")
MyStream.Open
MyStream.Type = 1
MyStream.LoadFromFile fileurl

'读取文件类型,让系统识别,以存为不同类型的文件。
Select Case lcase(Right(fName, 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 ".asp"
 ContentType = "text/html"
  Case ".txt"
 ContentType = "text/plain"
  Case Else
 ContentType = "application/octet-stream"
  End Select

'下载
Response.AddHeader "Content-Disposition", "attachment; filename=" & fName
Response.AddHeader "Content-Length", fsize
Response.Charset = "UTF-8"
Response.ContentType = ContentType
Response.BinaryWrite MyStream.Read
Response.Flush

'释放MyStream
MyStream.Close
Set MyStream = Nothing
%>

<a href="dl.asp?dlfile=xp0045.jpg">xxx</a>

原创粉丝点击