NB----NB大牛------请进-----下载大小问题

li367321 li367321 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
这几天一直折腾下载的一个东西,一直有下载不了大文件的念头闪烁,今天终于深测了下。发现文件大概到了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
INFO ,12-03-21 13:31:21,ForeignFileToZIP,创建文件夹D:\download\外来文件_31868e20-3a43-419d-b6f4-39425736f2d4\外来文件
INFO ,12-03-21 13:31:21,ToExcel,生成说明文档
INFO ,12-03-21 13:31:24,SetExcelTitle,设置说明文档EXCEL的表头
INFO ,12-03-21 13:31:58,ForeignFileToZIP,说明文档生成完成,存放:D:\download\外来文件_31868e20-3a43-419d-b6f4-39425736f2d4\外来文件\外来文件.xls
INFO ,12-03-21 13:31:58,ForeignFileToZIP,读取文件,整理文件存放目录
INFO ,12-03-21 13:32:34,ForeignFileToZIP,压缩文件
INFO ,12-03-21 13:36:02,ForeignFileToZIP,下载成功
INFO ,12-03-21 13:36:02,ForeignFileToZIP,文件名:外来文件.zip
INFO ,12-03-21 13:36:02,ForeignFileToZIP,大小:756758941
INFO ,12-03-21 13:36:02,ForeignFileToZIP,大小length:10000
INFO ,12-03-21 13:36:02,ForeignFileToZIP,大小length:10000
INFO ,12-03-21 13:36:02,ForeignFileToZIP,大小length:10000
INFO ,12-03-21 13:36:02,ForeignFileToZIP,大小length:10000
INFO ,12-03-21 13:36:02,ForeignFileToZIP,大小length:10000
INFO ,12-03-21 13:36:02,ForeignFileToZIP,大小length:10000
INFO ,12-03-21 13:36:02,ForeignFileToZIP,大小length:10000
INFO ,12-03-21 13:36:02,ForeignFileToZIP,大小length:10000
INFO ,12-03-21 13:36:02,ForeignFileToZIP,大小length:10000
INFO ,12-03-21 13:36:02,ForeignFileToZIP,大小length:10000
INFO ,12-03-21 13:36:02,ForeignFileToZIP,大小length:10000
INFO ,12-03-21 13:36:02,ForeignFileToZIP,大小length:10000
INFO ,12-03-21 13:36:02,ForeignFileToZIP,大小length:10000
INFO ,12-03-21 13:36:02,ForeignFileToZIP,大小length:10000
INFO ,12-03-21 13:45:21,ForeignFileToZIP,下载成功!
INFO ,12-03-21 13:47:49,Application_Start,ASP.global_asax,Application_Start
INFO ,12-03-21 13:47:49,Application_Start,ASP.global_asax,IsCache:true
...全文

相关推荐

评论 5
收藏 已收藏
分享
返回顶部
li367321
.NET社区 62,271 成员 668,968 内容