当前位置:K88软件开发文章中心编程语言AspAsp01 → 文章内容

asp下载文件的源代码

减小字体 增大字体 作者:佚名  来源:翔宇亭IT乐园  发布时间:2019-1-3 0:43:38

:2010-08-03 12:14:00

我们可以使用ASP编写下载文件的源代码,可以隐藏文件名,识别不同的文件类型,通过修改后可以保存到数据库中。

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%><%Response.codepage="65001"Response.Charset="utf-8"%><!--#include file="easp/easp.asp"--><%Response.Buffer=truedim 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 = nothingend subFunction 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 = NothingEnd FunctionFunction 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)'清空BufferResponse.Buffer = TrueResponse.Clear'创建Stream对象Set s = Server.CreateObject("ADODB.Stream")s.Open'设置流对象为二进制类型s.Type = 1on error resume next'检测文件是否存在Set fso = Server.CreateObject("Scripting.FileSystemObject")If Not fso.FileExists(strFilename) ThendownloadFile="NoFile"Exit FunctionEnd If'计算文件长度Set f = fso.GetFile(strFilename)intFilelength = f.sizeIf filename="" Thenfilename=f.nameEnd Ifs.LoadFromFile(strFilename)if err thenResponse.Write("<h1>Error: </h1>" & err.Description & "<p>")Response.Endend if'向用户浏览器发送HeaderResponse.AddHeader "Content-Disposition", "attachment; filename=" & filenameResponse.AddHeader "Content-Length", intFilelengthResponse.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.FlushLoop'清理s.CloseSet s = NothingEnd Function%>


asp下载文件的源代码