asp.net 文件下載功能函數(shù)代碼整理
更新時(shí)間:2010年04月03日 16:54:35 作者:
asp.net下文件下載功能代碼,fullFilename 要下載的文件的路徑+文件名,需要的朋友可以參考下。
復(fù)制代碼 代碼如下:
public void FileDownLoadDel(string fullFilename)
{
System.IO.Stream iStream = null;
// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[10000];
// Length of the file:
int length;
// Total bytes to read:
long dataToRead;
// Identify the file to download including its path.
string filepath = fullFilename;
filepath = Server.MapPath(filepath);
// Identify the file name.
string filename = System.IO.Path.GetFileName(filepath);
try
{
// Open the file.
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
System.IO.FileAccess.Read, System.IO.FileShare.Read);
// Total bytes to read:
dataToRead = iStream.Length;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
// Read the bytes.
while (dataToRead > 0)
{
// Verify that the client is connected.
if (Response.IsClientConnected)
{
// Read the data in buffer.
length = iStream.Read(buffer, 0, 10000);
// Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length);
// Flush the data to the HTML output.
Response.Flush();
buffer = new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
Response.Clear();
}
}
Response.End(); //沒(méi)有這句會(huì)將該頁(yè)面刷新后的內(nèi)容追加寫(xiě)入文件中。
}
catch (Exception ex)
{
// Trap the error, if any.
//Response.Write("Error : " + ex.Message);
//base.WriteLog("資料", "下載資料:" + ex.Message + "!", LogType.Error, this.GetType().ToString());
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();
}
File.Delete(fullFilename);
}
}
相關(guān)文章
.NET?Core類(lèi)庫(kù)項(xiàng)目中讀取appsettings.json配置的方法
ASP.NET?Core是一個(gè)全新的Web開(kāi)發(fā)平臺(tái),微軟在它上面構(gòu)建了MVC、SingalR、GRPC、Orleans這樣廣泛使用的Web框架,今天通過(guò)本文給大家詳細(xì)介紹下.NET?Core讀取appsettings.json配置的方法,感興趣的朋友一起看看吧2022-03-03
如何處理ASP.NET Core中HTML5客戶(hù)端路由回退的問(wèn)題
這篇文章主要給大家介紹了關(guān)于如何處理ASP.NET Core中HTML5客戶(hù)端路由回退的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
.Net筆記:System.IO之windows文件操作的深入分析
本篇文章是對(duì).Net中windows文件操作的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
.NET生成動(dòng)態(tài)驗(yàn)證碼的完整步驟
這篇文章主要給大家介紹了關(guān)于.NET生成動(dòng)態(tài)驗(yàn)證碼的完整步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用.NET具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
.Net實(shí)現(xiàn)上傳圖片按比例自動(dòng)縮小或放大的方法
這篇文章主要介紹了.Net實(shí)現(xiàn)上傳圖片按比例自動(dòng)縮小或放大的方法,實(shí)例內(nèi)容簡(jiǎn)潔功能實(shí)用,需要的朋友可以參考下2014-09-09
Asp.Net 無(wú)刷新文件上傳并顯示進(jìn)度條的實(shí)現(xiàn)方法及思路
這篇文章詳細(xì)介紹了無(wú)刷新文件上傳并顯示進(jìn)度條的思路和代碼,有需要的朋友可以參考一下2013-06-06
增加asp.net應(yīng)用程序性能的20種方法(簡(jiǎn)單有效)
增加asp.net應(yīng)用程序性能的20種方法小結(jié),需要的朋友可以參考下,對(duì)于服務(wù)器也需要一些設(shè)置。2010-01-01

