ASP.NET中下載文件的幾種實(shí)例代碼
//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 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");
//'文件名稱
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();
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
--保存文檔
string docName, docExtended;
Stream doc = fuDoc.FileContent;
int docLength = fuDoc.PostedFile.ContentLength;
byte[] docData = new byte[docLength];
doc.Read(docData, 0, docLength);
docExtended = fuDoc.FileName.Substring(fuDoc.FileName.IndexOf("."));
if (string.IsNullOrEmpty(tbDoc.Text))
docName = fuDoc.FileName;
else
docName = tbDoc.Text + docExtended;
SafetyDocInfo data = new SafetyDocInfo(ddlSort.Text, docName, fuDoc.PostedFile.ContentType, docData);
SafetyDoc safety = new SafetyDoc();
safety.Insert(data);
tbDoc.Text = string.Empty;
--打開(kāi)文檔
public void ViewSafetyDoc(string pDocSort, string pDocName)
{
OracleParameter[] parms = GetSafetyDocParm(SQL_View_SafetyDoc);
parms[0].Value = pDocSort;
parms[1].Value = pDocName;
using (OracleDataReader rdr = OracleHelper.ExecuteReader(OracleHelper.ConnectionString, CommandType.Text, SQL_View_SafetyDoc, parms))
{
while (rdr.Read())
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType = rdr.GetString(1);
HttpContext.Current.Response.BinaryWrite((byte[])rdr["Doc"]);
string FileName = rdr.GetString(0);
FileName = System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8);
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
}
}
}
- ASP.NET Web Api 2實(shí)現(xiàn)多文件打包并下載文件的實(shí)例
- ASP.NET(C#) Web Api通過(guò)文件流下載文件的實(shí)例
- ASP.NET批量下載文件的方法
- ASP.NET 在下載文件時(shí)對(duì)其重命名的思路及實(shí)現(xiàn)方法
- asp.net C#實(shí)現(xiàn)下載文件的六種方法實(shí)例
- 在ASP.NET中下載文件的實(shí)現(xiàn)代碼
- asp.net BackgroundWorker之在后臺(tái)下載文件
- asp.net 下載文件時(shí)根據(jù)MIME類(lèi)型自動(dòng)判斷保存文件的擴(kuò)展名
- asp.net 下載文件時(shí)輸出文件內(nèi)容
- asp.net Web Services上傳和下載文件(完整代碼)
- ASP.NET實(shí)現(xiàn)從服務(wù)器下載文件問(wèn)題處理
相關(guān)文章
充分利用ASP.NET的三種緩存提高站點(diǎn)性能的注意方法
充分利用ASP.NET的三種緩存提高站點(diǎn)性能的注意方法...2007-09-09
asp.net 在線編輯word文檔 可保存到服務(wù)器
使用說(shuō)明:該方法只在office xp 和 2003上 測(cè)試通過(guò),2000及以下 版本沒(méi)試。2010-01-01
.NETCore基于RabbitMQ實(shí)現(xiàn)延時(shí)隊(duì)列的兩方法
這篇文章主要介紹了.NETCore基于RabbitMQ實(shí)現(xiàn)延時(shí)隊(duì)列的兩方法,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
Asp.net下拉樹(shù)的實(shí)現(xiàn)過(guò)程
這篇文章主要介紹了Asp.net下拉樹(shù)的實(shí)現(xiàn)過(guò)程,文章思路清晰,推薦給大家閱讀,需要的朋友可以參考下2015-08-08
手把手教你在.NET中創(chuàng)建Web服務(wù)實(shí)現(xiàn)方法
這篇文章主要介紹了.NET中創(chuàng)建Web服務(wù)實(shí)現(xiàn)方法,有需要的朋友可以參考一下2013-12-12
ASP.NET MVC中將控制器分離到類(lèi)庫(kù)的實(shí)現(xiàn)
這篇文章主要介紹了ASP.NET MVC中將控制器分離到類(lèi)庫(kù)的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2015-06-06
ASP.NET與ASP互通COOKIES的一點(diǎn)經(jīng)驗(yàn)
ASP與ASP.NET互相整合時(shí),其中文COOKIES信息無(wú)法被互通共享,當(dāng)使用ASP.NET寫(xiě)入中文COOKIES信息后,使用ASP進(jìn)行讀取,讀出來(lái)的卻是亂碼,而非中文。2010-03-03
ASP.NET Core優(yōu)雅的在開(kāi)發(fā)環(huán)境保存機(jī)密(User Secrets)
這篇文章主要為大家詳細(xì)介紹了ASP.NET Core如何優(yōu)雅的在開(kāi)發(fā)環(huán)境保存機(jī)密User Secrets,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05

