熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

asp.net下載文件幾種方式

2022-06-13   來源: .NET編程 
    一//TransmitFile實現下載
   
    protectedvoid Button_Click(object sender EventArgs e)
   
    {
   
    /*
   
    微軟為Response對象提供了一個新的方法TransmitFile來解決使用ResponseBinaryWrite
   
    下載超過mb的文件時導致Aspnet_wpexe進程回收而無法成功下載的問題
   
    代碼如下
   
    */
   
    ResponseContentType =application/xzipcompressed;
   
    ResponseAddHeader(ContentDisposition attachment;filename=zzip
   
    string filename = ServerMapPath(DownLoad/zzip
   
    ResponseTransmitFile(filename)
   
    }
   
    二//WriteFile實現下載
   
    protectedvoid Button_Click(object sender EventArgs e)
   
    {
   
    /*
   
    using SystemIO;
   
    */
   
    string fileName =asdtxt;//客戶端保存的文件名
   
    string filePath = ServerMapPath(DownLoad/aaatxt//路徑
   
    FileInfo fileInfo =new FileInfo(filePath)
   
    ResponseClear()
   
    ResponseClearContent()
   
    ResponseClearHeaders()
   
    ResponseAddHeader(ContentDisposition attachment;filename=+ fileName)
   
    ResponseAddHeader(ContentLength fileInfoLengthToString())
   
    ResponseAddHeader(ContentTransferEncoding binary
   
    ResponseContentType =application/octetstream;
   
    ResponseContentEncoding = SystemTextEncodingGetEncoding(gb
   
    ResponseWriteFile(fileInfoFullName)
   
    ResponseFlush()
   
    ResponseEnd()
   
    }


   
    三 //WriteFile分塊下載
   
    protectedvoid Button_Click(object sender EventArgs e)
   
    {
   
    string fileName =aaatxt;//客戶端保存的文件名
   
    string filePath = ServerMapPath(DownLoad/aaatxt//路徑
   
    SystemIOFileInfo fileInfo =new SystemIOFileInfo(filePath)
   
    if (fileInfoExists ==true)
   
    {
   
    constlong ChunkSize =;//K 每次讀取文件只讀取K這樣可以緩解服務器的壓力
   
    byte[] buffer =newbyte[ChunkSize];
   
    ResponseClear()
   
    SystemIOFileStream iStream = SystemIOFileOpenRead(filePath)
   
    long dataLengthToRead = iStreamLength;//獲取下載的文件總大小
   
    ResponseContentType =application/octetstream;
   
    ResponseAddHeader(ContentDisposition attachment; filename=+ HttpUtilityUrlEncode(fileName))
   
    while (dataLengthToRead >&& ResponseIsClientConnected)
   
    {
   
    int lengthRead = iStreamRead(buffer ConvertToInt(ChunkSize))//讀取的大小
   
    ResponseOutputStreamWrite(buffer lengthRead)
   
    ResponseFlush()
   
    dataLengthToRead = dataLengthToRead lengthRead;
   
    }
   
    ResponseClose()
   
    }
   
    }
   
    四//流方式下載
   
    protectedvoid Button_Click(object sender EventArgs e)
   
    {
   
    string fileName =aaatxt;//客戶端保存的文件名
   
    string filePath = ServerMapPath(DownLoad/aaatxt//路徑
   
    //以字符流的形式下載文件
   
    FileStream fs =new FileStream(filePath FileModeOpen)
   
    byte[] bytes =newbyte[(int)fsLength];
   
    fsRead(bytes bytesLength)
   
    fsClose()
   
    ResponseContentType =application/octetstream;
   
    //通知浏覽器下載文件而不是打開
   
    ResponseAddHeader(ContentDisposition attachment; filename=+ HttpUtilityUrlEncode(fileName SystemTextEncodingUTF))
   
    ResponseBinaryWrite(bytes)
   
    ResponseFlush()
   
    ResponseEnd()
   
    }
   
    //
   
    publicvoid DownloadFile( SystemWebUIPage WebFormString FileNameWhenUserDownload String FileBody )
   
    {
   
    WebFormResponseClearHeaders()
   
    WebFormResponseClear()
   
    WebFormResponseExpires =;
   
    WebFormResponseBuffer =true;
   
    WebFormResponseAddHeader(AcceptLanguage zhtw
   
    //文件名稱
   
    WebFormResponseAddHeader(contentdisposition attachment; filename=+SystemWebHttpUtilityUrlEncode(FileNameWhenUserDownload SystemTextEncodingUTF)+
   
    WebFormResponseContentType =Application/octetstream;
   
    //文件內容
   
    WebFormResponseWrite(FileBody)//
   
    WebFormResponseEnd()
   
    }
   
    //上面這段代碼是下載一個動態產生的文本文件若這個文件已經存在於服務器端的實體路徑則可以通過下面的函數
   
    publicvoid DownloadFileByFilePath( SystemWebUIPage WebFormString FileNameWhenUserDownload String FilePath )
   
    {
   
    WebFormResponseClearHeaders()
   
    WebFormResponseClear()
   
    WebFormResponseExpires =;
   
    WebFormResponseBuffer =true;
   
    WebFormResponseAddHeader(AcceptLanguage zhtw
   
    //文件名稱
   
    WebFormResponseAddHeader(contentdisposition attachment; filename=+ SystemWebHttpUtilityUrlEncode(FileNameWhenUserDownload SystemTextEncodingUTF) +
   
    WebFormResponseContentType =Application/octetstream;
   
    //文件內容
   
    WebFormResponseWrite(SystemIOFileRea}dAllBytes(FilePath))//
   
    WebFormResponseEnd()
   
    }


From:http://tw.wingwit.com/Article/program/net/201311/12214.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.