asp中使用流方式下載檔案的代碼

來源:互聯網
上載者:User
將下面的內容存成download.asp 
然後你就可以用<a  herf="http://xxx.xxx.com/download.asp?n=file.doc">download!</a> 
來下載了! 
------------------------------------------------------------ 
<% 
Response.Buffer  =  true 
Response.Clear 
 
           dim  url 
           Dim  fso,fl,flsize 
           dim  Dname 
           Dim  objStream,ContentType,flName,isre,url1 
'*********************************************調用時傳入的下載檔案名稱 
           Dname=trim(request("n")) 
'****************************************************************** 
           If  Dname<>""  Then 
'******************************下載檔案存放的服務端目錄 
                       url=server.MapPath("/")&"/"&Dname 
'*************************************************** 
           End  If 
 
           Set  fso=Server.CreateObject("Scripting.FileSystemObject") 
                       Set  fl=fso.getfile(url) 
                       flsize=fl.size 
                       flName=fl.name 
                       Set  fl=Nothing 
                       Set  fso=Nothing 
           %> 
           <% 
                       Set  objStream  =  Server.CreateObject("ADODB.Stream") 
                       objStream.Open 
                       objStream.Type  =  1 
                       objStream.LoadFromFile  url 
 
 
                                   Select  Case  lcase(Right(flName,  4)) 
                                               Case  ".asf" 
                                                           ContentType  =  "video/x-ms-asf" 
                                               Case  ".avi" 
                                                           ContentType  =  "video/avi" 
                                               Case  ".doc" 
                                                           ContentType  =  "application/msword" 
                                               Case  ".zip" 
                                                           ContentType  =  "application/zip" 
                                               Case  ".xls" 
                                                           ContentType  =  "application/vnd.ms-excel" 
                                               Case  ".gif" 
                                                           ContentType  =  "image/gif" 
                                               Case  ".jpg",  "jpeg" 
                                                           ContentType  =  "image/jpeg" 
                                               Case  ".wav" 
                                                           ContentType  =  "audio/wav" 
                                               Case  ".mp3" 
                                                           ContentType  =  "audio/mpeg3" 
                                               Case  ".mpg",  "mpeg" 
                                                           ContentType  =  "video/mpeg" 
                                               Case  ".rtf" 
                                                           ContentType  =  "application/rtf" 
                                               Case  ".htm",  "html" 
                                                           ContentType  =  "text/html" 
                                               Case  ".txt" 
                                                           ContentType  =  "text/plain" 
                                               Case  Else 
                                                           ContentType  =  "application/octet-stream" 
                                   End  Select 
 
 
 
                                   Response.AddHeader  "Content-Disposition",  "attachment;  filename="  &  flName 
                                   Response.AddHeader  "Content-Length",  flsize 
 
                                   Response.Charset  =  "UTF-8" 
                                   Response.ContentType  =  ContentType 
 
                                   Response.BinaryWrite  objStream.Read 
                                   Response.Flush 
                                   response.Clear() 
                       objStream.Close 
                       Set  objStream  =  Nothing 
 
%> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.