![C# 從服務(wù)器下載代碼.docx_第1頁](http://file.renrendoc.com/FileRoot1/2020-1/5/716c134d-8313-428c-876f-1498c351db7d/716c134d-8313-428c-876f-1498c351db7d1.gif)
![C# 從服務(wù)器下載代碼.docx_第2頁](http://file.renrendoc.com/FileRoot1/2020-1/5/716c134d-8313-428c-876f-1498c351db7d/716c134d-8313-428c-876f-1498c351db7d2.gif)
![C# 從服務(wù)器下載代碼.docx_第3頁](http://file.renrendoc.com/FileRoot1/2020-1/5/716c134d-8313-428c-876f-1498c351db7d/716c134d-8313-428c-876f-1498c351db7d3.gif)
![C# 從服務(wù)器下載代碼.docx_第4頁](http://file.renrendoc.com/FileRoot1/2020-1/5/716c134d-8313-428c-876f-1498c351db7d/716c134d-8313-428c-876f-1498c351db7d4.gif)
![C# 從服務(wù)器下載代碼.docx_第5頁](http://file.renrendoc.com/FileRoot1/2020-1/5/716c134d-8313-428c-876f-1498c351db7d/716c134d-8313-428c-876f-1498c351db7d5.gif)
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rè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)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 貴州2025年貴州省衛(wèi)生健康委員會部分直屬事業(yè)單位招聘141人筆試歷年參考題庫附帶答案詳解
- 荊州2025年湖北荊州市市直事業(yè)單位人才引進388人筆試歷年參考題庫附帶答案詳解
- 河南河南省實驗幼兒園面向教育部直屬師范大學(xué)2025屆公費師范畢業(yè)生招聘筆試歷年參考題庫附帶答案詳解
- 2025年中國固體亞氯酸鈉市場調(diào)查研究報告
- 2025至2031年中國陶瓷型自動鞋套機行業(yè)投資前景及策略咨詢研究報告
- 2025年脫扣器自動拍打清洗機項目可行性研究報告
- 2025至2031年中國組合音響揚聲器行業(yè)投資前景及策略咨詢研究報告
- 2025年玻璃濾片包裝回收箱項目可行性研究報告
- 2025至2031年中國機車塑膠配件行業(yè)投資前景及策略咨詢研究報告
- 2025年手機沙發(fā)項目可行性研究報告
- 駐場保潔方案
- 中國心理衛(wèi)生協(xié)會家庭教育指導(dǎo)師參考試題庫及答案
- 智能廣告投放技術(shù)方案
- 知識產(chǎn)權(quán)保護執(zhí)法
- 高質(zhì)量社區(qū)建設(shè)的路徑與探索
- 數(shù)字化時代的酒店員工培訓(xùn):技能升級
- 足球守門員撲救技巧:撲救結(jié)合守護球門安全
- 《學(xué)術(shù)規(guī)范和論文寫作》課件全套 第1-10章 知:認(rèn)識研究與論文寫作 - 引文規(guī)范
- 起重機更換卷筒施工方案
- 01智慧物流信息技術(shù)概述
- 精神發(fā)育遲滯的護理查房
評論
0/150
提交評論