NB----NB大牛------请进-----下载大小问题
这几天一直折腾下载的一个东西,一直有下载不了大文件的念头闪烁,今天终于深测了下。发现文件大概到了600多M的时候,客户端就没反应了,一直响应着。服务器端日志显示下载完成
下面贴上源码:
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 = fileName + ".zip";
// Identify the file name.
string filename = System.IO.Path.GetFileName(filepath);
Logger.Log.Info("文件名:"+filename);
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;
Logger.Log.Info("大小:" + dataToRead);
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename="+Server.UrlEncode(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);
Logger.Log.Info("大小length:" + length);
// 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;
}
}
Logger.Log.Info("下载成功!");
}
catch (Exception ex)
{
// Trap the error, if any.
Logger.Log.Info("异常:" + ex.ToString());
//Response.Write("Error : " + ex.Message);
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();
}
}
日志:
INFO ,12-03-21 13:31:20,Page_Load,页面加载中........
INFO ,12-03-21 13:31:20,Page_Load,执行文件打包操作
INFO ,12-03-21 13:31:21,ForeignFileToZIP,执行SP_RPT_GETFILEDOWNLOAD获取文件的信息900
INFO ,12-03-21 13:31:21,ForeignFileToZIP,执行SP_RPT_GetFilesToExcel获取说明文档信息900<