- ·上一篇内容:使用javascript去掉字符串首尾的空格
- ·下一篇内容:iis出现不支持此接口错误的处理方法
asp下载文件的源代码
我们可以使用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 %>
本文源自:翔宇亭——IT乐园(http://www.biye5u.com),转载请保留此信息!
评论内容只代表网友观点,与本站立场无关!
评论摘要(共 0 条,得分 0 分,平均 0 分)
查看完整评论