zoukankan      html  css  js  c++  java
  • 用ASP实现文件下载

    <% 
    '**************************************************************
    '**使用方法:                                                **
    '**在点击下载处加连接<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:Inetpubwwwrootpichelp.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
    %>

  • 相关阅读:
    macOS下查看端口是否被占用命令
    macOS下安装tomcat服务器
    unittest 知识问答题-也许面试会有用-欢迎补充
    python.unittest中常用断言方法
    如何在命令行terminal中运行pycharm项目
    如何保证 软件测试覆盖率
    如何衡量 测试覆盖率
    软件测试全流程 总结
    接口测试对传统测试模型的改进
    关于http/https协议
  • 原文地址:https://www.cnblogs.com/bit5566/p/5671554.html
Copyright © 2011-2022 走看看