.net中下載文件的實(shí)例代碼
一、//TransmitFile實(shí)現(xiàn)下載
protected void Button_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 byte[ChunkSize];
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");
//'文件名稱(chēng)
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");
//文件名稱(chēng)
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.Rea}dAllBytes(FilePath));//---------
WebForm.Response.End();
}
相關(guān)文章
asp.net 網(wǎng)頁(yè)動(dòng)態(tài)查詢條件的實(shí)現(xiàn)
最近有一個(gè)需求,會(huì)在 mongodb 中插入各種類(lèi)型的數(shù)據(jù),算是記錄業(yè)務(wù)日志的數(shù)據(jù)庫(kù)吧。因?yàn)闃I(yè)務(wù)對(duì)象類(lèi)型都不同,所以插入的數(shù)據(jù)格式也完全不同2012-10-10asp.net 購(gòu)物車(chē)的實(shí)現(xiàn)淺析
我從來(lái)沒(méi)有進(jìn)行過(guò)正式的web開(kāi)發(fā),但是我一直喜歡web,所以這篇文章也是我轉(zhuǎn)行web的一個(gè)開(kāi)始吧。或多或少我也參考了幾個(gè)網(wǎng)站的實(shí)現(xiàn)(當(dāng)然了,只是看看大概的功能而已),所以也請(qǐng)大家多多指教。2011-02-02asp.net頁(yè)面SqlCacheDependency緩存實(shí)例
這篇文章主要介紹了asp.net頁(yè)面SqlCacheDependency緩存實(shí)例,以一個(gè)完整實(shí)例來(lái)展現(xiàn)asp.net中緩存技術(shù)的使用方法,需要的朋友可以參考下2014-08-08asp.net(C#)實(shí)現(xiàn)功能強(qiáng)大的時(shí)間日期處理類(lèi)完整實(shí)例
這篇文章主要介紹了asp.net(C#)實(shí)現(xiàn)功能強(qiáng)大的時(shí)間日期處理類(lèi),封裝了針對(duì)日期與時(shí)間的各種常用的判斷與計(jì)算功能,非常方便實(shí)用,需要的朋友可以參考下2016-06-06Asp.net使用SignalR實(shí)現(xiàn)酷炫端對(duì)端聊天功能
這篇文章主要為大家詳細(xì)介紹了Asp.net使用SignalR實(shí)現(xiàn)酷炫端對(duì)端聊天功能,感興趣的小伙伴們可以參考一下2016-04-04asp.net 錯(cuò)誤:0x8007000B 異常的解決方法
這篇文章主要介紹了asp.net 錯(cuò)誤:0x8007000B 異常的解決方法,需要的朋友可以參考下2015-01-01Coolite Cool Study 2 同時(shí)更新多個(gè)Tab
前段時(shí)間有一個(gè)需求是這樣子的——錄入一個(gè)查詢條件, 出來(lái)的查詢結(jié)果是多張頁(yè)面。不知道有沒(méi)朋友遇到這個(gè)問(wèn)題。 展現(xiàn)的效果大概是這個(gè)樣子2009-05-05Asp.net MVC中使用JQuery插件ajaxFileUpload上傳文件
這篇文章主要介紹了Asp.net MVC中使用JQuery插件ajaxFileUpload上傳文件,需要的朋友可以參考下2016-08-08