asp下载文件代码总结

ASP 1448 0 2013-12-20

asp下载文件代码总结
 挺好用的,整理收藏起来.

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Response.codepage="65001"
Response.Charset="utf-8"
%>
<!--#include file="easp/easp.asp"-->
<%
Response.Buffer=true

dim url,trueurl

   url=Easp.Ext("myfunc").objdetailinfo("download","id",Easp.Get("id")).item("downfile")
  sz=instrrev(PATH,"\")+1
  fname=mid(path,sz)
   call DownloadFile(url,fname)
'call downfile(server.MapPath(url))
sub downfile(path)
 response.CLeaR
 Set osm = server.createobject("adodb.stream")
 osm.open
 osm.TypE = 1
 osm.loadfromfile path

 sz=instrrev(PATH,"\")+1
  select case lcase(right(sz,4))
      case ".asf"
      response.ContentType="video/x-ms-asf"
      case ".avi"
      response.ContentType="video/avi"
      case ".doc"
      response.ContentType="application/msword"
      case ".zip"
      response.ContentType="application/zip"
      case ".xls"
      response.ContentType="application/vnd.ms-excel"
      case ".gif"
      response.ContentType="image/gif"
      case ".jpg","jpeg"
      response.ContentType="image/jpeg"
      case ".wav"
      response.ContentType="audio/wav"
      case ".mp3"
      response.ContentType="audio/mpeg3"
      case ".mpg", "mpeg"
      response.ContentType="video/mpeg"
      case ".rtf"
     response.ContentType="application/rtf"
      case ".htm","html"
      response.ContentType="text/html"
      case ".txt"
      response.ContentType="text/plain"
  Case ".ASP", ".ASA", "ASPX", "ASAX", ".MDB"
        Response.Write "受保护文件,不能下载."
        Response.End
      case else
      response.ContentType="appliation/octet-stream"
   end select
 response.addheader "Content-Disposition", "attachment; filename=" & mid(path,sz)
 response.addheader "Content-Length", osm.size
 response.charset = "UTF-8"
 'response.contenttype = "application/octet-stream"
 response.binarywrite osm.read
 response.flush
 osm.close
 set osm = nothing
end sub

Function getFileInfo(FileName)
    Dim FSO, File, FileInfo(10)
    Set FSO = Server.CreateObject("Scripting.FileSystemObject")
    If FSO.FileExists(Server.MapPath(FileName)) Then
        Set File = FSO.GetFile(Server.MapPath(FileName))
        FileInfo(0)=File.Size
        If FileInfo(0)>1024 Then
          FileInfo(0)=Round(FileInfo(0) / 1024,2)
        If FileInfo(0) > 1024 Then
          FileInfo(0)=Round(FileInfo(0) / 1024,2)
          FileInfo(0)= FileInfo(0) & " MB"
        Else
         FileInfo(0)= FileInfo(0) & " KB"
        End If
        Else
          FileInfo(0)= FileInfo(0) & " Byte"
        End If
        FileInfo(1) = LCase(Right(FileName, 4))
        FileInfo(2) = File.DateCreated
        FileInfo(3) = File.Type
        FileInfo(4) = File.DateLastModified
        FileInfo(5) = File.Path
        FileInfo(6) = "" 'File.ShortPath 部分服务器不支持
        FileInfo(7) = File.Name
        FileInfo(8) = "" 'File.ShortName 部分服务器不支持
        FileInfo(9) = FSO.getExtensionName(Server.MapPath(FileName))
        FileInfo(10) = File.DateLastModified
    End If
    getFileInfo = FileInfo
    Set FSO = Nothing
End Function

Function DownloadFile(strFile,filename)
'Program:54powerman
'email:54powerman@163.com
'HomePage: http://blog.sina.com.cn/u/1055000490
'Writen:2003-4
'确保系统安装了最新版的MDAC
' -------------------------------------------------------------
strFilename = server.MapPath(strFile)
'清空Buffer
Response.Buffer = True
Response.Clear
'创建Stream对象
Set s = Server.CreateObject("ADODB.Stream")
s.Open
'设置流对象为二进制类型
s.Type = 1
on error resume next
'检测文件是否存在
Set fso = Server.CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(strFilename) Then
downloadFile="NoFile"
Exit Function
End If
'计算文件长度
Set f = fso.GetFile(strFilename)
intFilelength = f.size
If filename="" Then
filename=f.name
End If
s.LoadFromFile(strFilename)
if err then
Response.Write("<h1>Error: </h1>" & err.Description & "<p>")
Response.End
end if
'向用户浏览器发送Header
Response.AddHeader "Content-Disposition", "attachment; filename=" & filename
Response.AddHeader "Content-Length", intFilelength
Response.CharSet = "UTF-8"
Response.ContentType = "application/octet-stream"
'输出文件
'对于小于4096KB的文件可以用语句
'Response.BinaryWrite s.Read
'Response.Flush
'完成,但对于大于4096KB的文件要分段输出,如下循环操作。
Do While Not s.EOS
 Contents = s.Read (4096) '每次读取4096KB
 Response.BinaryWrite Contents
 Response.Flush
Loop

'清理
s.Close
Set s = Nothing
End Function

%>

上一篇:asp清除缓存的三个方法

下一篇:ASP实现点击链接直接下载文件的代码

讨论数量:0

请先登录再发表讨论。 2024-05-03

天涯网魂
3 杠 5 星
TA 的文章
TA 的随言
TA 的资源链