检查一下是不是带宽不够?
带宽肯定没问题
你好,
看看是否可以在网络硬件上为设置一下为每一个Request设置下最大限额的流量.
也不应该是这个问题,因为我限速到10kb了
这是下载代码
try
{
FileStream file = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(file);
try
{
response.AddHeader("Accept-Ranges", "bytes");
response.Buffer = false;
long fileLength = file.Length;
long startBytes = 0;
int pack = 10240; //10K bytes
int sleep = (int)Math.Floor(1000.0 * pack / speed) + 1;
if (request.Headers["Range"] != null)
{
response.StatusCode = 206;
string[] range = request.Headers["Range"].Split(new char[] { '=', '-' });
startBytes = Convert.ToInt64(range[1]);
}
response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
if ((startBytes != 0))
{
response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
}
response.AddHeader("Connection", "Keep-Alive");
response.ContentType = "application/octet-stream";
response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int)Math.Floor(1.0 * (fileLength - startBytes) / pack) + 1;
for (int i = 0; i <= maxCount; i++)
{
if ((response.IsClientConnected))
{
response.BinaryWrite(br.ReadBytes(pack));
System.Threading.Thread.Sleep(sleep);
}
else
{
i = maxCount;
}
}
}
catch
{
return false;
}
finally
{
br.Close();
file.Close();
}
}
catch (Exception ex)
{
return false;
}
@王长委:
请问是IIS 6.0么?
@dotNetDR_:
恩,是iis 6.0
@王长委:
昨晚写了一部分实现.在此带上截图.测试环境为单机,Web服务器承载是IIS Express,应该比理想值不会差距太远
<!--web.config下的部分配置-->
<appSettings>
<!--下载的文件路径-->
<add key="fileFullPath" value="D:\TDDOWNLOAD\mx3.1.3.2000cn.exe"/>
<!--每次发送的包的大小(单位:kb)-->
<add key="packSize" value="50"/>
<!--休眠时间(单位:毫秒[注:10000毫秒 = 1秒])-->
<add key="sleep" value="500"/>
</appSettings>
// DownloadFile.ashx
<%@ WebHandler Language="C#" CodeBehind="DownloadFile.ashx.cs" Class="WebDownload.DownloadFile" %>
//-----------------------------------------------
// DownloadFile.ashx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.IO;
using System.Configuration;
namespace WebDownload
{
/// <summary>
/// Summary description for DownloadFile
/// 参考:http://www.codedigest.com/CodeDigest/39-File-Download-in-ASP-Net-with-C-.aspx
/// </summary>
public class DownloadFile : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
string fileFullPath = ConfigurationManager.AppSettings["fileFullPath"];
int packSize = int.Parse(ConfigurationManager.AppSettings["packSize"]);
packSize *= 1024; //1024你懂的
Download(context, fileFullPath, packSize);
}
public bool IsReusable
{
get
{
return false;
}
}
public bool Download(HttpContext context,
string fileFullPath,
int packSize)
{
try
{
HttpResponse response = context.Response;
HttpRequest request = context.Request;
string fileName = Path.GetFileName(fileFullPath);
fileName = HttpUtility.UrlEncode(fileName, Encoding.UTF8);
using (Stream stream = new FileStream(fileFullPath,
FileMode.Open,
FileAccess.Read,
FileShare.Read))
{
#region 断点续传判断
string requestHeadRange = request.Headers["Range"];
if (requestHeadRange != null)
{
response.StatusCode = 206;
string[] rang = requestHeadRange.Split(new char[] { '=', '-' });
long offset = long.Parse(rang[1]);
stream.Seek(offset, SeekOrigin.Current);
}
#endregion
long bytesToRead = stream.Length;
response.ContentType = "application/octet-stream";
response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
response.AddHeader("Content-Length", bytesToRead.ToString());
int sleep = int.Parse(ConfigurationManager.AppSettings["sleep"]);
while (bytesToRead > 0)
{
if (response.IsClientConnected)
{
byte[] buffer = new Byte[packSize];
int length = stream.Read(buffer, 0, packSize);
response.OutputStream.Write(buffer, 0, packSize);
response.Flush();
System.Threading.Thread.Sleep(sleep);
bytesToRead = bytesToRead - length;
}
}
return true;
}
}
catch (Exception ex)
{
//这里自己记录异常信息了
return false;
}
}
}
}
1个Chrome和2个IE在同时下载没发现LZ说的慢的问题.
以下为配置信息的理论值,非常符合预期值了.
如果对你有帮助,请给予答案.谢谢~
@dotNetDR_:
跟我的代码不一样吗?
@王长委:
是这样的.你代码里
int sleep = (int)Math.Floor(1000.0 * pack / speed) + 1;
int maxCount = (int)Math.Floor(1.0 * (fileLength - startBytes) / pack) + 1;
System.Threading.Thread.Sleep(sleep);
我看不出为什么.而且又没注释.所以就自己去写一个看看是否对你有帮助了.~在说你的代码...风格跟我自己用的风格不太一样~
你先试试我的代码看看能否为你解决问题在来反馈一下.我在做相应调整看看.
我也是第一次回复断点续传的.~自己简陋地测试了一下.还没发现问题.
@dotNetDR_:
谢谢你的帮助,不过你的代码和我的代码是一样的
@王长委:
什么?问题还是依旧?还是一样慢?
@dotNetDR_:
先Remark好~自己没有能力解决这个问题而导致问题被关闭.~~
以后会谨慎处理这类问题的.~
继续前进.....直到接近尽头!
做个ftp,把下载文件放到ftp里,然后把页面下载url改成ftp的地址,这样可以控制每个文件的下载流量。
最好把ftp服务器和http服务器分开,方面监控和分压。
不懂