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
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
[解决办法]
分块下载.
参考。
- C# code
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; } } }}
[解决办法]
http://topic.csdn.net/u/20120308/10/0582c285-4cc6-4876-8f90-829fa1caf1f7.html希望对楼主有帮助 ,,,不行的话 楼主再查查 。。