首页 > Web开发 > 详细

用ASP实现文件下载

时间:2016-07-14 21:07:26      阅读:286      评论:0      收藏:0      [点我收藏+]

<% 
‘**************************************************************
‘**使用方法:                                                **
‘**在点击下载处加连接<a href="load.asp?filename=文件名"></a> **
‘**阳光白雪——2005年11月21日                                **
‘**E-mail:chenmanyi0818@126.com                              **
‘**HomePage:http://www.toumh.com ;                          **
‘**************************************************************
‘**************************************************
‘**              实现文件下载函数                **
‘**************************************************
Function Filedownload(filename) 
  Dim strchar,fliesend,objectFile,objfile,objStream,path,pathtype
  pathtype = "application/x-msdownload" 
  path = server.MapPath(filename)
  Const loadfilesize=32768 ‘32KB,也可取其它值,单位:字节
  ‘在给path赋值后,其最终值必定是该图片在服务器端存储器上的绝对路径,如 C:\Inetpub\wwwroot\pic\help.gif
  ‘因为 server.MapPath 取得的只是站点根目录的路径,所以在后面还应加上文件所在的文件夹再加文件名
  ‘例如:要下载的文件 setup.exe 在根目录的 download 文件夹下,则 path = server.MapPath("download/"&filename)
  ‘***********************************
  ‘测试用
  ‘response.Write(path)
  ‘response.Write(filename)
  ‘response.End()
  ‘***********************************
  fliesend=0 
  TransferFile = True 
  Set objectFile = Server.CreateObject("Scripting.FileSystemObject") 
  Set objfile = objectFile.GetFile(Path) 
  Set objStream = objfile.OpenAsTextStream(1,-1) 
  Response.AddHeader "content-type", pathtype 
  response.AddHeader "Content-Disposition","attachment;filename="&filename 
  Response.AddHeader "content-length", objfile.Size 
   Do While Not objStream.AtEndOfStream 
     strchar = objStream.Read(1) 
     Response.BinaryWrite(strchar) 
     fliesend = fliesend + 1 
     If (fliesend MOD loadfilesize) = 0 Then 
       Response.Flush 
       If Not Response.IsClientConnected Then 
         TransferFile = False 
         Exit Do 
       End If 
     End If 
   Loop
  Response.Flush 
  If Not Response.IsClientConnected Then
    TransferFile = False
  end if
  objStream.Close 
  Set objStream = Nothing 
  Set objectFile = Nothing 
End Function 
‘****************************************************
‘**               文件下载函数结束                 **
‘****************************************************
Dim fileneme,downloadfile 
filename = request("filename")  ‘此处的 filename 仅为下载文件的名称(包括扩展名)
downloadfile = Filedownload(filename) ‘调用文件下载函数
Response.End
%>

用ASP实现文件下载

原文:http://www.cnblogs.com/bit5566/p/5671554.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!