asp.net中文件下載功能的實(shí)例代碼
//TransmitFile實(shí)現(xiàn)下載
protected void Button1_Click(object sender, EventArgs e)
{
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename=z.zip");
string filename = Server.MapPath("DownLoad/aaa.zip");
Response.TransmitFile(filename);
}
//WriteFile實(shí)現(xiàn)下載
protected void Button2_Click(object sender, EventArgs e)
{
string fileName ="aaa.zip";//客戶端保存的文件名
string filePath=Server.MapPath("DownLoad/aaa.zip");//路徑
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.zip";//客戶端保存的文件名
string filePath = Server.MapPath("DownLoad/aaa.zip");//路徑
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.zip";//客戶端保存的文件名
string filePath = Server.MapPath("DownLoad/aaa.zip");//路徑
//以字符流的形式下載文件
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();
}
- Asp.net實(shí)現(xiàn)MVC處理文件的上傳下載功能實(shí)例教程
- ASP.NET Core實(shí)現(xiàn)文件上傳和下載
- ASP.NET?Core實(shí)現(xiàn)動(dòng)態(tài)獲取文件并下載
- asp.net實(shí)現(xiàn)多個(gè)文件同時(shí)下載功能
- ASP.NET實(shí)現(xiàn)從服務(wù)器下載文件問(wèn)題處理
- asp.net實(shí)現(xiàn)服務(wù)器文件下載到本地的方法
- ASP.Net下載大文件的實(shí)現(xiàn)方法
- ASP.NET 在下載文件時(shí)對(duì)其重命名的思路及實(shí)現(xiàn)方法
- 在ASP.NET中下載文件的實(shí)現(xiàn)代碼
- asp.net 文件下載實(shí)現(xiàn)代碼
- ASP.NET MVC實(shí)現(xiàn)文件下載
相關(guān)文章
ASP.NET MVC 導(dǎo)出Word報(bào)表
本文主要介紹了ASP.NET MVC 導(dǎo)出Word報(bào)表的方法,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02
WPF實(shí)現(xiàn)文本描邊+外發(fā)光效果的示例代碼
這篇文章主要介紹了如何利用WPF實(shí)現(xiàn)文本描邊以及外發(fā)光的效果,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定幫助,需要的可以參考一下2022-03-03
ASP.NET?Core應(yīng)用JWT進(jìn)行用戶認(rèn)證及Token的刷新方案
本文詳細(xì)講解了ASP.NET?Core應(yīng)用JWT進(jìn)行用戶認(rèn)證及Token的刷新方案,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12
.NET Core類庫(kù)System.Reflection.DispatchProxy實(shí)現(xiàn)簡(jiǎn)易Aop的方法
這篇文章主要給大家介紹了關(guān)于.NET Core類庫(kù)System.Reflection.DispatchProxy實(shí)現(xiàn)簡(jiǎn)易Aop的相關(guān)資料,文中通過(guò)示例代碼結(jié)束的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
ASP.NET MVC懶加載如何逐步加載數(shù)據(jù)庫(kù)信息
在ASP.NET MVC中實(shí)現(xiàn)數(shù)據(jù)庫(kù)的逐步加載可通過(guò)懶加載技術(shù)完成,首先,在EntityFramework中配置數(shù)據(jù)庫(kù)上下文,使用對(duì)應(yīng)的實(shí)體類映射數(shù)據(jù)庫(kù)表,本文給大家介紹ASP.NET MVC懶加載如何逐步加載數(shù)據(jù)庫(kù)信息,感興趣的朋友跟隨小編一起看看吧2024-10-10
ASP.NET 2.0服務(wù)器控件開發(fā)之復(fù)雜屬性
ASP.NET 2.0服務(wù)器控件開發(fā)之復(fù)雜屬性...2006-09-09
CheckBoxList兩列并排編譯為表格顯示具體實(shí)現(xiàn)
CheckBoxList兩列并排的顯示效果相比大家都有見(jiàn)到過(guò)吧,下面是具體的實(shí)現(xiàn)代碼,感興趣的朋友可以參考下哈2013-05-05
ASP.NET連接數(shù)據(jù)庫(kù)并獲取數(shù)據(jù)方法總結(jié)
這篇文章主要介紹了ASP.NET連接數(shù)據(jù)庫(kù)并獲取數(shù)據(jù)方法,結(jié)合實(shí)例分析總結(jié)了ASP.NET連接數(shù)據(jù)庫(kù)及獲取數(shù)據(jù)的相關(guān)實(shí)現(xiàn)技巧,并附帶了web.config配置文件的使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2015-11-11

