散分顺便问一个ASP.NET大文件下载的问题。总的来说我只知道三种方法,一个是直接用超连接绑定到给文件的路径,第二个是Response.WriteFile Response.TransmitFile 第三个是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 = "DownloadFileName";// 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;
}
}
} catch (Exception ex) {
// Trap the error, if any.
Response.Write("Error : " + ex.Message);
} finally {
if (iStream != null) {
//Close the file.
iStream.Close();
}
}
下面说下个人的看法,不对的地方大家指出来
我感觉第一个方法缺点是 无法在程序中控制下载权限,无法统计下载信息,第二个下载方法貌似说下载大文件时候很占系统内存。第三个方法貌似是解决了第二个方法的不足,但是貌似不支持第三方软件的下载如迅雷。 请各位DX说下有什么好一点的方法没、有没有 能够控制下载权限 统计下载信息 下载大文件的时候不会出这样那样的问题 然后能够用下载软件下载的方法没。
最后在祝大家新年快乐,在新的一年里心想事成。。
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 = "DownloadFileName";// 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;
}
}
} catch (Exception ex) {
// Trap the error, if any.
Response.Write("Error : " + ex.Message);
} finally {
if (iStream != null) {
//Close the file.
iStream.Close();
}
}
下面说下个人的看法,不对的地方大家指出来
我感觉第一个方法缺点是 无法在程序中控制下载权限,无法统计下载信息,第二个下载方法貌似说下载大文件时候很占系统内存。第三个方法貌似是解决了第二个方法的不足,但是貌似不支持第三方软件的下载如迅雷。 请各位DX说下有什么好一点的方法没、有没有 能够控制下载权限 统计下载信息 下载大文件的时候不会出这样那样的问题 然后能够用下载软件下载的方法没。
最后在祝大家新年快乐,在新的一年里心想事成。。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货