asp.net 文件下載功能函數(shù)代碼整理
更新時間:2010年04月03日 16:54:35 作者:
asp.net下文件下載功能代碼,fullFilename 要下載的文件的路徑+文件名,需要的朋友可以參考下。
復制代碼 代碼如下:
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(); //沒有這句會將該頁面刷新后的內(nèi)容追加寫入文件中。
}
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類庫項目中讀取appsettings.json配置的方法
ASP.NET?Core是一個全新的Web開發(fā)平臺,微軟在它上面構(gòu)建了MVC、SingalR、GRPC、Orleans這樣廣泛使用的Web框架,今天通過本文給大家詳細介紹下.NET?Core讀取appsettings.json配置的方法,感興趣的朋友一起看看吧2022-03-03
如何處理ASP.NET Core中HTML5客戶端路由回退的問題
這篇文章主要給大家介紹了關(guān)于如何處理ASP.NET Core中HTML5客戶端路由回退的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2017-11-11
.Net筆記:System.IO之windows文件操作的深入分析
本篇文章是對.Net中windows文件操作的使用進行了詳細的分析介紹,需要的朋友參考下2013-05-05
Asp.Net 無刷新文件上傳并顯示進度條的實現(xiàn)方法及思路
這篇文章詳細介紹了無刷新文件上傳并顯示進度條的思路和代碼,有需要的朋友可以參考一下2013-06-06

