




已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
C# 從服務(wù)器下載文件代碼一、/TransmitFile實現(xiàn)下載 protected void Button1_Click(object sender, EventArgs e)/* 微軟為Response對象提供了一個新的方法TransmitFile來解決使用Response.BinaryWrite 下載超過400mb的文件時導(dǎo)致Aspnet_wp.exe進程回收而無法成功下載的問題。 代碼如下: */ Response.ContentType = application/x-zip-compressed;Response.AddHeader(Content-Disposition, attachment;filename=z.zip);string filename = Server.MapPath(DownLoad/z.zip);Response.TransmitFile(filename);二、/WriteFile實現(xiàn)下載 protected void Button2_Click(object sender, EventArgs e)/* using System.IO;*/string fileName = asd.txt;/客戶端保存的文件名 string filePath = Server.MapPath(DownLoad/aaa.txt);/路徑FileInfo fileInfo = new FileInfo(filePath);Response.Clear();Response.ClearContent();Response.ClearHeaders();Response.AddHeader(Content-Disposition, attachment;filename= + fileName);Response.AddHeader(Content-Length, fileInfo.Length.ToString();Response.AddHeader(Content-Transfer-Encoding, binary);Response.ContentType = application/octet-stream;Response.ContentEncoding = System.Text.Encoding.GetEncoding(gb2312);Response.WriteFile(fileInfo.FullName);Response.Flush();Response.End();三、 /WriteFile分塊下載 protected void Button3_Click(object sender, EventArgs e)string fileName = aaa.txt;/客戶端保存的文件名 string filePath = Server.MapPath(DownLoad/aaa.txt);/路徑System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);if (fileInfo.Exists = true)const long ChunkSize = 102400;/100K 每次讀取文件,只讀取100K,這樣可以緩解服務(wù)器的壓力 byte buffer = new byteChunkSize;Response.Clear();System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);long dataLengthToRead = iStream.Length;/獲取下載的文件總大小 Response.ContentType = application/octet-stream;Response.AddHeader(Content-Disposition, attachment; filename= + HttpUtility.UrlEncode(fileName);while (dataLengthToRead 0 & Response.IsClientConnected)int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize);/讀取的大小 Response.OutputStream.Write(buffer, 0, lengthRead);Response.Flush();dataLengthToRead = dataLengthToRead - lengthRead;Response.Close();四、/流方式下載 protected void Button4_Click(object sender, EventArgs e)string fileName = aaa.txt;/客戶端保存的文件名 string filePath = Server.MapPath(DownLoad/aaa.txt);/路徑/以字符流的形式下載文件 FileStream fs = new FileStream(filePath, FileMode.Open);byte bytes = new byte(int)fs.Length;fs.Read(bytes, 0, bytes.Length);fs.Close();Response.ContentType = application/octet-stream;/通知瀏覽器下載文件而不是打開 Response.AddHeader(Content-Disposition, attachment; filename= + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);Response.BinaryWrite(bytes);Response.Flush();Response.End();/-public void DownloadFile( System.Web.UI.Page WebForm,String FileNameWhenUserDownload ,String FileBody )WebForm.Response.ClearHeaders();WebForm.Response.Clear();WebForm.Response.Expires = 0;WebForm.Response.Buffer = true;WebForm.Response.AddHeader(Accept-Language, zh-tw);/文件名稱WebForm.Response.AddHeader(content-disposition, attachment; filename=+System.Web.HttpUtility.UrlEncode(FileNameWhenUserDownload, System.Text.Encoding.UTF8)+);WebForm.Response.ContentType = Application/octet-stream;/文件內(nèi)容WebForm.Response.Write(FileBody);/-WebForm.Response.End();/上面這段代碼是下載一個動態(tài)產(chǎn)生的文本文件,若這個文件已經(jīng)存在于服務(wù)器端的實體路徑,則可以通過下面的函數(shù):public void DownloadFileByFilePath( System.Web.UI.Page WebForm,String FileNameWhenUserDownload ,String FilePath )WebForm.Response.ClearHeaders();WebForm.Response.Clear();WebForm.Response.Expires = 0;WebForm.Response.Buffer = true;WebForm.Response.AddHeader(Accept-Language, zh-tw);/文件名稱WebForm.Response.AddHeader(content-disposition, attachment; filename= + System.Web.HttpUtility.UrlEncode(FileNameWhenUserDownload, System.Text.Encoding.UTF8) + );WebForm.Response.ContentType = Application/octet-stream;/文件內(nèi)容WebForm.Response.Write(System.IO.File.ReadAllBytes(FilePath);/-WebForm.Response.End(); 一、/TransmitFile實現(xiàn)下載 protected void Button1_Click(object sender, EventArgs e)/* 微軟為Response對象提供了一個新的方法TransmitFile來解決使用Response.BinaryWrite 下載超過400mb的文件時導(dǎo)致Aspnet_wp.exe進程回收而無法成功下載的問題。 代碼如下: */ Response.ContentType = application/x-zip-compressed;Response.AddHeader(Content-Disposition, attachment;filename=z.zip);string filename = Server.MapPath(DownLoad/z.zip);Response.TransmitFile(filename);二、/WriteFile實現(xiàn)下載 protected void Button2_Click(object sender, EventArgs e)/* using System.IO;*/string fileName = asd.txt;/客戶端保存的文件名 string filePath = Server.MapPath(DownLoad/aaa.txt);/路徑FileInfo fileInfo = new FileInfo(filePath);Response.Clear();Response.ClearContent();Response.ClearHeaders();Response.AddHeader(Content-Disposition, attachment;filename= + fileName);Response.AddHeader(Content-Length, fileInfo.Length.ToString();Response.AddHeader(Content-Transfer-Encoding, binary);Response.ContentType = application/octet-stream;Response.ContentEncoding = System.Text.Encoding.GetEncoding(gb2312);Response.WriteFile(fileInfo.FullName);Response.Flush();Response.End();三、 /WriteFile分塊下載 protected void Button3_Click(object sender, EventArgs e)string fileName = aaa.txt;/客戶端保存的文件名 string filePath = Server.MapPath(DownLoad/aaa.txt);/路徑System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);if (fileInfo.Exists = true)const long ChunkSize = 102400;/100K 每次讀取文件,只讀取100K,這樣可以緩解服務(wù)器的壓力 byte buffer = new byteChunkSize;Response.Clear();System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);long dataLengthToRead = iStream.Length;/獲取下載的文件總大小 Response.ContentType = application/octet-stream;Response.AddHeader(Content-Disposition, attachment; filename= + HttpUtility.UrlEncode(fileName);while (dataLengthToRead 0 & Response.IsClientConnected)int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize);/讀取的大小 Response.OutputStream.Write(buffer, 0, lengthRead);Response.Flush();dataLengthToRead = dataLengthToRead - lengthRead;Response.Close();四、/流方式下載 protected void Button4_Click(object sender, EventArgs e)string fileName = aaa.txt;/客戶端保存的文件名 string filePath = Server.MapPath(DownLoad/aaa.txt);/路徑/以字符流的形式下載文件 FileStream fs = new FileStream(filePath, FileMode.Open);byte bytes = new byte(int)fs.Length;fs.Read(bytes, 0, bytes.Length);fs.Close();Response.ContentType = application/octet-stream;/通知瀏覽器下載文件而不是打開 Response.AddHeader(Content-Disposition, attachment; filename= + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);Response.BinaryWrite(bytes);Response.Flush();Response.End();/-public void DownloadFile( System.Web.UI.Page WebForm,String FileNameWhenUserDownload ,String FileBody )WebForm.Response.ClearHeaders();WebForm.Response.Clear();WebForm.Response.Expires = 0;WebForm.Response.Buffer = true;WebForm.Response.AddHeader(Accept-Language, zh-tw);/文件名稱WebForm.Response.AddHeader(content-disposition, attachment; filename=+System.Web.HttpUtility.UrlEncode(FileNameWhenUserDownload, System.Text.Encoding.UTF8)+);WebForm.Response.ContentT
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年江蘇客運從業(yè)資格考試題庫及答案
- 2025年廣州出租車從業(yè)資格證考題
- 社工服務(wù)培訓(xùn)課件
- 安檢機培訓(xùn)課件
- 心電監(jiān)護的題目及答案
- 小組話題題目大全及答案
- 小學(xué)應(yīng)用題目及答案
- 三角形全等幾何模型-一線三等角模型(基礎(chǔ)篇)
- 江蘇省農(nóng)業(yè)農(nóng)村廳事業(yè)單位真題2024
- 2024年廣西三支一扶考試真題
- 聯(lián)塑管材檢驗報告模板
- 高鈉血癥護理查房
- 小學(xué)數(shù)學(xué)練習(xí)設(shè)計的有效性研究結(jié)題報告
- DL∕T 5776-2018 水平定向鉆敷設(shè)電力管線技術(shù)規(guī)定
- 汕頭市龍湖區(qū)2021年教師招聘《教育公共知識》試題及答案
- 浙江溫州十校2023至2024學(xué)年高二下學(xué)期6月期末聯(lián)考化學(xué)試題附參考答案(解析)
- 語文-山東省淄博市2023-2024學(xué)年高二下學(xué)期7月期末教學(xué)質(zhì)量檢測試題和答案
- 湖南省婁底市漣源市2023-2024學(xué)年六年級下學(xué)期6月期末英語試題
- 上海市徐匯區(qū)市級名校2025屆物理高一第二學(xué)期期末考試模擬試題含解析
- 天一大聯(lián)盟2024屆高一數(shù)學(xué)第二學(xué)期期末統(tǒng)考試題含解析
- 【語文】西安外國語大學(xué)附屬小學(xué)(雁塔區(qū))小學(xué)五年級下冊期末試卷(含答案)
評論
0/150
提交評論