asp下载文件的源代码

时间:2021-05-02

我们可以使用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.CLeaRSet osm = server.createobject("adodb.stream")osm.openosm.TypE = 1osm.loadfromfile pathsz=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 selectresponse.addheader "Content-Disposition", "attachment; filename=" & mid(path,sz)response.addheader "Content-Length", osm.sizeresponse.charset = "UTF-8"'response.contenttype = "application/octet-stream"response.binarywrite osm.readresponse.flushosm.closeset 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.EOSContents = s.Read (4096) '每次读取4096KBResponse.BinaryWrite ContentsResponse.FlushLoop'清理s.CloseSet s = NothingEnd Function%>本文源自:翔宇亭——IT乐园(http://www.biye5u.com),转载请保留此信息!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章