国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net 文件下載功能函數代碼整理

2024-07-10 13:26:08
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:


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(); //沒有這句會將該頁面刷新后的內容追加寫入文件中。
}
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);
}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 绵阳市| 广安市| 云南省| 广河县| 泾川县| 阿鲁科尔沁旗| 宝坻区| 宿松县| 桂平市| 松阳县| 改则县| 安仁县| 沙洋县| 滨海县| 柏乡县| 平武县| 昌图县| 阿拉尔市| 时尚| 毕节市| 民丰县| 惠东县| 玉田县| 任丘市| 大英县| 洛浦县| 石河子市| 额尔古纳市| 略阳县| 东城区| 张家川| 崇文区| 府谷县| 新干县| 嘉善县| 横山县| 张掖市| 东乌珠穆沁旗| 金门县| 紫阳县| 天门市|