




已閱讀5頁(yè),還剩3頁(yè)未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(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來(lái)解決使用Response.BinaryWrite 下載超過(guò)400mb的文件時(shí)導(dǎo)致Aspnet_wp.exe進(jìn)程回收而無(wú)法成功下載的問(wè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;/通知瀏覽器下載文件而不是打開(kāi) 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í)體路徑,則可以通過(guò)下面的函數(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來(lái)解決使用Response.BinaryWrite 下載超過(guò)400mb的文件時(shí)導(dǎo)致Aspnet_wp.exe進(jìn)程回收而無(wú)法成功下載的問(wè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;/通知瀏覽器下載文件而不是打開(kāi) 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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝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ù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 超市采購(gòu)年度工作總結(jié)
- DB43-T 2857-2023‘金絲皇菊’栽培技術(shù)規(guī)程
- 人教版七年級(jí)下期末專題復(fù)習(xí)專題四 實(shí)數(shù)期末提升卷(含解析)
- 眼內(nèi)炎護(hù)理查房
- 肛癰的護(hù)理方案
- 戰(zhàn)術(shù)威力測(cè)試題及答案
- 人教版部編版七年級(jí)語(yǔ)文下冊(cè)阿長(zhǎng)與山海經(jīng)
- 品德教育小班課件
- 腫瘤專科護(hù)士職業(yè)發(fā)展路徑規(guī)劃
- 2025年藥品及醫(yī)療器械批發(fā)服務(wù)項(xiàng)目申請(qǐng)報(bào)告
- 聯(lián)塑管材檢驗(yàn)報(bào)告模板
- 高鈉血癥護(hù)理查房
- 小學(xué)數(shù)學(xué)練習(xí)設(shè)計(jì)的有效性研究結(jié)題報(bào)告
- 汕頭市龍湖區(qū)2021年教師招聘《教育公共知識(shí)》試題及答案
- 浙江溫州十校2023至2024學(xué)年高二下學(xué)期6月期末聯(lián)考化學(xué)試題附參考答案(解析)
- 語(yǔ)文-山東省淄博市2023-2024學(xué)年高二下學(xué)期7月期末教學(xué)質(zhì)量檢測(cè)試題和答案
- 湖南省婁底市漣源市2023-2024學(xué)年六年級(jí)下學(xué)期6月期末英語(yǔ)試題
- 上海市徐匯區(qū)市級(jí)名校2025屆物理高一第二學(xué)期期末考試模擬試題含解析
- 天一大聯(lián)盟2024屆高一數(shù)學(xué)第二學(xué)期期末統(tǒng)考試題含解析
- 【語(yǔ)文】西安外國(guó)語(yǔ)大學(xué)附屬小學(xué)(雁塔區(qū))小學(xué)五年級(jí)下冊(cè)期末試卷(含答案)
- 小學(xué)數(shù)學(xué)“組題”設(shè)計(jì)分析 論文
評(píng)論
0/150
提交評(píng)論