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

li367321 2012-03-21 02:06:47
这几天一直折腾下载的一个东西,一直有下载不了大文件的念头闪烁,今天终于深测了下。发现文件大概到了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
...全文
108 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
SomethingJack 2012-03-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 dongt1 的回复:]

我前几天问过c#的下载 有个大侠告诉我了 再我的问题表里面 ==我给你找找 哈。。。
[/Quote]
dongt1 2012-03-22
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20120308/10/0582c285-4cc6-4876-8f90-829fa1caf1f7.html希望对楼主有帮助 ,,,不行的话 楼主再查查 。。
dongt1 2012-03-22
  • 打赏
  • 举报
回复
我前几天问过c#的下载 有个大侠告诉我了 再我的问题表里面 ==我给你找找 哈。。。
SomethingJack 2012-03-21
  • 打赏
  • 举报
回复
分块下载.
参考。

using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace Asiastar.NR.Ajax
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Handler1 : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";

string id = context.Request["id"].ToString();//获取资源的编号
System.IO.Stream iStream = null;
byte[] buffer = new Byte[10000];
int length;
long dataToRead;
NRBLL.File bf = new Asiastar.NRBLL.File();
Guid guid = new Guid(id);
if (bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FilePath"] != null)//判断数据库路径是否存在
{
string filepath = bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FilePath"].ToString();//获取资源完整路径 D:\资源文件\600cc139-14cf-448e-9e50-daa972d35e01.jpg
string Oidfilename = bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FileNam"].ToString();//旧文件名称
//string filename = System.IO.Path.GetFileName(filepath);//获取文件名称+后缀名 600cc139-14cf-448e-9e50-daa972d35e01.JPG
//int index = filepath.IndexOf(".");
//string filetype = filepath.Substring(index).ToLower();//后缀名
//string newfilename = Oidfilename;
//string filepath1 = bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FilePath"].ToString().Substring(0,filepath.Length - 8);
try
{
string fileName = HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(Oidfilename));//解码(注意这里2层解码)
Oidfilename = Oidfilename.Replace("+", "%20"); //将“+”替换成“空格”
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
dataToRead = iStream.Length;
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Oidfilename, System.Text.Encoding.UTF8)); //下载的时候下载原来的文件名称
while (dataToRead > 0)
{
if (context.Response.IsClientConnected)
{
length = iStream.Read(buffer, 0, 10000);
context.Response.OutputStream.Write(buffer, 0, length);
context.Response.Flush();
buffer = new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
dataToRead = -1;
}
}

}
catch (Exception ex)
{
NR.Error.Log.LogType(ex.ToString());
}
finally
{
if (iStream != null)
{
iStream.Close();
}
}
}
else
{
NR.Error.Log.LogType("找不到文件!");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}

porschev 2012-03-21
  • 打赏
  • 举报
回复

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧