用ASP實現檔案下載

來源:互聯網
上載者:User

標籤:

<% 
‘**************************************************************
‘**使用方法:                                                **
‘**在點擊下載處加串連<a href="load.asp?filename=檔案名稱"></a> **
‘**陽光白雪——2005年11月21日                                **
‘**E-mail:[email protected]                              **
‘**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實現檔案下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.