【ASP】下载文件代码

程序<a href=代码"/> 程序代码

<%  
call downloadFile(replace(Request("file"),"",""))  
  
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 exist<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>" & err.Description & "<p>")  
   Response.End  
   end if  
  
' send the headers to the users browser  
   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  
  
%>

调用
<a href="down.asp?file=/upfiles/FlexCell_NET_Full_V2(5UU43-J3M38-9GI5P-83EP8-LQ18R).rar">download</a>
原创文章如转载,请注明转载自 NextCool [ http://www.nextcool.cn ]
本文地址:http://www.nextcool.cn/article.asp?id=161
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: asp
评论: 2 | 引用: 0 | 查看次数: 4770
  • 1
wmwqq [2010-01-10 05:50 PM]
谢谢!正好用上
cooldonet [2008-02-02 11:04 AM]
谢谢,代码帮助了我.
  • 1
发表评论
昵 称:
密 码: 游客发言不需要密码.
验证码: 验证码
内 容:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 开启