对文件下载带宽限制 [英] Bandwidth throttling for file download

查看:372
本文介绍了对文件下载带宽限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面code,限制文件下载速度的应用;

I've following code to limit file download speed for an application;

context.Response.Buffer = false;
context.Response.AppendHeader("Content-Disposition",
                              "attachment;filename=" + arquivo.Nome);
context.Response.AppendHeader("Content-Type",
                              "application/octet-stream");
context.Response.AppendHeader("Content-Length",
                               arquivo.Tamanho.ToString());

int offset = 0;
byte[] buffer = new byte[currentRange.OptimalDownloadRate];

while (context.Response.IsClientConnected && offset < arquivo.Tamanho)
{
    DateTime start = DateTime.Now;
    int readCount = arquivo.GetBytes(buffer, offset, // == .ExecuteReader()
        (int)Math.Min(arquivo.Tamanho - offset, buffer.Length));
    context.Response.OutputStream.Write(buffer, 0, readCount);
    offset += readCount;
    CacheManager.Hit(jobId, fileId.ToString(), readCount, buffer.Length, null);

    TimeSpan elapsed = DateTime.Now - start;
    if (elapsed.TotalMilliseconds < 1000)
    {
        Thread.Sleep(1000 - (int)elapsed.TotalMilliseconds);
    }
}

一如往常,它工作正常,进入我的发展,内部及客户QA环境,但它抛出一个异常,在生产环境中:

As always, it works fine into my development, internal and customer QA environments, but it's throwing an exception in production environment:

System.Threading.ThreadAbortException: Thread was being aborted.
   at System.Threading.Thread.SleepInternal(Int32 millisecondsTimeout)
   at (...).Handlers.DownloadHandler.processDownload(HttpContext context, ...)

有关用户,一个新的窗口,在下载对话框:

For user, a new window opens upon download dialog:

The connection with the server was reset

你有任何想法是怎么回事?

Do you have any idea what's going on?

推荐答案

问题是,要求更多的超过90秒运行。

Problem was that request running for more than 90 seconds.

我想改变这个HTTP处理程序实施 IHttpAsyncHandler 并创建一个背景下,非阻塞线程。现在,一切工作正常。

I'd alter that HTTP handler to implement IHttpAsyncHandler and create a background, non-blocking thread. Now everything works fine.

这篇关于对文件下载带宽限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆