首页 新闻 搜索 专区 学院

asp.net网站一有人下载,网站速度就很慢怎么解决?

0
悬赏园豆:200 [已关闭问题] 关闭于 2011-09-06 17:03

asp.net网站一有人下载,网站速度就很慢怎么解决?

王长委的主页 王长委 | 初学一级 | 园豆:115
提问于:2011-09-03 11:07
< >
分享
所有回答(3)
0

检查一下是不是带宽不够?

dudu | 园豆:37797 (高人七级) | 2011-09-03 11:36

带宽肯定没问题

支持(0) 反对(0) 王长委 | 园豆:115 (初学一级) | 2011-09-05 15:42
0

你好,

看看是否可以在网络硬件上为设置一下为每一个Request设置下最大限额的流量.

dotNetDR_ | 园豆:2058 (老鸟四级) | 2011-09-03 12:42

也不应该是这个问题,因为我限速到10kb了

支持(0) 反对(0) 王长委 | 园豆:115 (初学一级) | 2011-09-05 15:43

这是下载代码

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;
            }

支持(0) 反对(0) 王长委 | 园豆:115 (初学一级) | 2011-09-05 15:44

@王长委:

请问是IIS 6.0么?

支持(0) 反对(0) dotNetDR_ | 园豆:2058 (老鸟四级) | 2011-09-05 15:45

@dotNetDR_:

恩,是iis  6.0

支持(0) 反对(0) 王长委 | 园豆:115 (初学一级) | 2011-09-05 16:17

@王长委:

昨晚写了一部分实现.在此带上截图.测试环境为单机,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说的慢的问题.

以下为配置信息的理论值,非常符合预期值了.

支持(0) 反对(0) dotNetDR_ | 园豆:2058 (老鸟四级) | 2011-09-06 13:09

如果对你有帮助,请给予答案.谢谢~

支持(0) 反对(0) dotNetDR_ | 园豆:2058 (老鸟四级) | 2011-09-06 13:11

@dotNetDR_:

跟我的代码不一样吗?

支持(0) 反对(0) 王长委 | 园豆:115 (初学一级) | 2011-09-06 14:22

@王长委:

是这样的.你代码里

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);

我看不出为什么.而且又没注释.所以就自己去写一个看看是否对你有帮助了.~在说你的代码...风格跟我自己用的风格不太一样~

你先试试我的代码看看能否为你解决问题在来反馈一下.我在做相应调整看看.

我也是第一次回复断点续传的.~自己简陋地测试了一下.还没发现问题.

支持(0) 反对(0) dotNetDR_ | 园豆:2058 (老鸟四级) | 2011-09-06 14:26

@dotNetDR_:

 谢谢你的帮助,不过你的代码和我的代码是一样的

支持(0) 反对(0) 王长委 | 园豆:115 (初学一级) | 2011-09-06 17:02

@王长委:

什么?问题还是依旧?还是一样慢?

支持(0) 反对(0) dotNetDR_ | 园豆:2058 (老鸟四级) | 2011-09-06 17:04

@dotNetDR_:

先Remark好~自己没有能力解决这个问题而导致问题被关闭.~~

以后会谨慎处理这类问题的.~

继续前进.....直到接近尽头!

支持(0) 反对(0) dotNetDR_ | 园豆:2058 (老鸟四级) | 2011-09-07 16:41
0

做个ftp,把下载文件放到ftp里,然后把页面下载url改成ftp的地址,这样可以控制每个文件的下载流量。

最好把ftp服务器和http服务器分开,方面监控和分压。

lookatmebaby | 园豆:320 (菜鸟二级) | 2011-09-04 16:46

不懂

支持(0) 反对(0) 王长委 | 园豆:115 (初学一级) | 2011-09-05 15:44
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册