




已閱讀5頁,還剩3頁未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
C# 從服務(wù)器下載文件代碼一、/TransmitFile實(shí)現(xiàn)下載 protected void Button1_Click(object sender, EventArgs e)/* 微軟為Response對象提供了一個新的方法TransmitFile來解決使用Response.BinaryWrite 下載超過400mb的文件時導(dǎo)致Aspnet_wp.exe進(jìn)程回收而無法成功下載的問題。 代碼如下: */ 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實(shí)現(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í)體路徑,則可以通過下面的函數(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實(shí)現(xiàn)下載 protected void Button1_Click(object sender, EventArgs e)/* 微軟為Response對象提供了一個新的方法TransmitFile來解決使用Response.BinaryWrite 下載超過400mb的文件時導(dǎo)致Aspnet_wp.exe進(jìn)程回收而無法成功下載的問題。 代碼如下: */ 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實(shí)現(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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 江蘇省南京市六校聯(lián)合體2024-2025學(xué)年高一下學(xué)期期末調(diào)研政治試卷(含答案)
- 廣東省茂名市電白區(qū)2024-2025學(xué)年九年級下學(xué)期期中道德與法治試題(含答案)
- 肺動脈栓塞術(shù)后護(hù)理常規(guī)
- 地產(chǎn)渠道市場培訓(xùn)課件
- 促動培訓(xùn)課件怎么寫
- 計(jì)劃生育法律法規(guī)培訓(xùn)
- 廣播員培訓(xùn)內(nèi)容
- 展示項(xiàng)目培訓(xùn)數(shù)據(jù)
- 生產(chǎn)安全培訓(xùn)資料
- 肺炎個案查房獲獎案例解析
- 網(wǎng)絡(luò)直播生態(tài)構(gòu)建-洞察分析
- 勞務(wù)分包工程施工組織設(shè)計(jì)
- 2025年吉林省國資委出資企業(yè)招聘筆試參考題庫含答案解析
- 2025年全國安全生產(chǎn)月安全生產(chǎn)知識競賽考試題庫及答案(共四套)
- 基于MATLABsimulink同步發(fā)電機(jī)突然三相短路仿真
- 《標(biāo)準(zhǔn)的制定》課件
- 國土空間規(guī)劃環(huán)評培訓(xùn)
- 風(fēng)動鑿巖機(jī)操作規(guī)程(4篇)
- 四川省成都市九縣區(qū)2023-2024學(xué)年高一下學(xué)期期末調(diào)研考試化學(xué)試題(解析版)
- (完整版)python學(xué)習(xí)課件
- 聯(lián)塑管材檢驗(yàn)報(bào)告模板
評論
0/150
提交評論