




已閱讀5頁(yè),還剩3頁(yè)未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
C# 從服務(wù)器下載文件代碼一、/TransmitFile實(shí)現(xiàn)下載 protected void Button1_Click(object sender, EventArgs e)/* 微軟為Response對(duì)象提供了一個(gè)新的方法TransmitFile來解決使用Response.BinaryWrite 下載超過400mb的文件時(shí)導(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();/上面這段代碼是下載一個(gè)動(dòng)態(tài)產(chǎn)生的文本文件,若這個(gè)文件已經(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對(duì)象提供了一個(gè)新的方法TransmitFile來解決使用Response.BinaryWrite 下載超過400mb的文件時(shí)導(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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年度河北省護(hù)師類之護(hù)士資格證能力測(cè)試試卷A卷附答案
- 2024年度河北省護(hù)師類之護(hù)士資格證每日一練試卷A卷含答案
- 2024年河北邯鄲成安縣事業(yè)單位招聘工作人員255名筆試備考題庫(kù)及完整答案詳解1套
- 山東省五蓮縣2024-2025學(xué)年高二下學(xué)期3月月考物理試題(解析版)
- 湖北省2024-2025學(xué)年高一下學(xué)期4月期中聯(lián)考物理試題(解析版)
- 江蘇省鹽城市聯(lián)盟校2024-2025學(xué)年高二下學(xué)期第二次階段性考試語(yǔ)文試題(含答案)
- 浙江省桐浦富興教研聯(lián)盟2024-2025學(xué)年高二下學(xué)期5月月考物理試題(掃描版含答案)
- 炸雞店的消費(fèi)者群體畫像
- 心理障礙患者護(hù)理
- 疾病傳播途徑與控制
- 畫冊(cè)設(shè)計(jì)制作報(bào)價(jià)單
- 借助數(shù)學(xué)實(shí)驗(yàn) 促進(jìn)思維發(fā)展
- 凈水廠畢業(yè)設(shè)計(jì)(圖紙+計(jì)算書)
- 河北工程大學(xué)食堂CI手冊(cè)
- 真空系統(tǒng)設(shè)計(jì)培訓(xùn)課件
- 機(jī)械設(shè)備維修的安全知識(shí)(課堂PPT)
- 住宅小區(qū)室外道路及管網(wǎng)配套工程施工方案
- 醫(yī)脈通三級(jí)綜合醫(yī)院服務(wù)能力指南2016年版
- 工區(qū)施工監(jiān)測(cè)監(jiān)測(cè)點(diǎn)保護(hù)管理辦法
- 泊船瓜洲集體備課
- 孔分子篩SBA-15的研究進(jìn)展
評(píng)論
0/150
提交評(píng)論