作家
登录

ASP实现文件直接下载的代码

作者: 来源:www.28hudong.com 2013-03-30 08:24:18 阅读 我要评论

<%@ language=vbscript codepage=65001%> <% 'Filename must be input if Request("Filename")="" then response.write "<h1>Error:</h1>Filename is empty!<p>" else call downloadFile(replace(replace(Request("Filename"),"",""),"/","")) Function downloadFile(strFile) ' make sure you are on the latest MDAC version for this to work ' get full path of specified file strFilename = server.MapPath(strFile) ' clear the buffer Response.Buffer = True Response.Clear ' create stream Set s = Server.CreateObject("ADODB.Stream") s.Open ' Set as binary s.Type = 1 ' load in the file on error resume next ' check the file exists Set fso = Server.CreateObject("Scripting.FileSystemObject") if not fso.FileExists(strFilename) then Response.Write("<h1>Error:</h1>"&strFilename&" does not exists!<p>") Response.End end if ' get length of file Set f = fso.GetFile(strFilename) intFilelength = f.size s.LoadFromFile(strFilename) if err then Response.Write("<h1>Error: </h1>Unknown Error!<p>") Response.End end if ' send the headers to the users Browse Response.AddHeader "Content-Disposition","attachment; filename="&f.name Response.AddHeader "Content-Length",intFilelength Response.CharSet = "UTF-8" Response.ContentType = "application/octet-stream" ' output the file to the browser Response.BinaryWrite s.Read Response.Flush ' tidy up s.Close Set s = Nothing End Function end if %>

  推荐阅读

  判断Session的过期时间 采用JavaScript实时显示剩余多少秒

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <%session.timeout=1440 '单位(分钟)1440=24小时 参数不能大于24小时 也就是不能大于1440分钟%> <% if session("login")<>"888888" then'判断Session是否有效 Response.W>>>详细阅读


本文标题:ASP实现文件直接下载的代码

地址:http://www.17bianji.com/kaifa2/ASP/32103.html

关键词: 探索发现

乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

网友点评
自媒体专栏

评论

热度

精彩导读
栏目ID=71的表不存在(操作类型=0)