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使用SkiaSharp實(shí)現(xiàn)快速生成二維碼
這篇文章主要為大家詳細(xì)介紹了.NET?Core如何使用SkiaSharp實(shí)現(xiàn)快速生成二維碼,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01ASP.NET文件上傳Upload的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了ASP.NET文件上傳的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11用WebClient.UploadData方法上載文件數(shù)據(jù)的方法
用WebClient.UploadData方法上載文件數(shù)據(jù)的方法...2007-04-04.net驗(yàn)證碼的刷新或局部刷新的方法實(shí)例
.net驗(yàn)證碼的刷新或局部刷新的方法實(shí)例,下面是實(shí)例,需要的朋友可以參考一下2013-03-03禁用aspx頁(yè)面的客戶(hù)端緩存(防止頁(yè)面被修改)
默認(rèn)情況下,IE打開(kāi)一個(gè)網(wǎng)頁(yè),會(huì)在本地進(jìn)行緩存,在某些時(shí)候也會(huì)帶來(lái)了弊端,比如修改信息的頁(yè)面等等因?yàn)閁RL并沒(méi)有改變,所以IE會(huì)讀取本地緩存,這種情況特別容易出現(xiàn)在彈出對(duì)話(huà)框或窗口進(jìn)行修改的方式感興趣的朋友可以了解下,或許對(duì)你有所幫助2013-02-02