指定内容长度时,大文件下载速度较慢 (IIS7)

发布于 2024-08-20 14:25:59 字数 658 浏览 8 评论 0原文

我有一个 asp.net 网页为用户提供大文件下载服务。 该页面托管在 IIS7、Windows Server 2008 上。

奇怪的是,当我不添加内容长度响应标头时,用户可以以良好的速度(2MB/s)下载,但一旦添加此标头,下载速度就会加快下降到 35kbps/s 左右。

这是代码:

Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/octet-stream";
//speed drops when I add this line:
//Response.AddHeader("Content-Length", new FileInfo(filepath).ToString());

Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
Response.TransmitFile(filepath);

Response.Flush();

当然我可以省略内容长度,但用户不会知道文件有多大以及下载需要多长时间......这很烦人。

知道什么会导致下载速度发生如此大的变化吗?

预先感谢您的任何见解!

I have an asp.net web page to serve large file downloads to users.
The page is hosted on IIS7, Windows Server 2008.

The strange thing is that users can download at good speeds (2MB/s) when I don't add a content-length response header but as soon as I add this header, download speed drops to somewhere around 35kbps/s.

This is the code:

Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/octet-stream";
//speed drops when I add this line:
//Response.AddHeader("Content-Length", new FileInfo(filepath).ToString());

Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
Response.TransmitFile(filepath);

Response.Flush();

Of course I can leave the content-length out but the user will not know how big the file is and how long the download will take...which is annoying.

Any idea what can cause this big change in download speed?

Thanks in advance for any insights!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沩ん囻菔务 2024-08-27 14:25:59

我最近使用了以下代码...

Response.AddHeader("Content-disposition", "attachment; filename=" + attachment.Filename);
Response.AddHeader("Content-length", attachment.Filedata.Length.ToString());
Response.ContentType = attachment.ContentType;
Response.OutputStream.Write(attachment.Filedata.ToArray(), 0, attachment.Filedata.Length);
Response.End();

(在本例中,我的附件实际上存储在数据库表中,但它只是将字节数组写入输出流)

而不是您的方法.. 。

Response.TransmitFile(filepath);

传输速度看起来还不错 我在几秒钟内就从 live 网站下载了 3.5MB。 (不仅仅是本地!)

我知道我应该使用 HttpHandler 而不是劫持响应,但这目前有效。另外,我可能应该分块读取字节数组,以避免占用太多内存。我会在某个时候回去并稍微修改一下。

因此,您可以尝试使用 Response.OutputStream.Write 或编写 HttpHandler 来代替。

无论如何,我希望这对你有帮助。

I've used the following code recently ...

Response.AddHeader("Content-disposition", "attachment; filename=" + attachment.Filename);
Response.AddHeader("Content-length", attachment.Filedata.Length.ToString());
Response.ContentType = attachment.ContentType;
Response.OutputStream.Write(attachment.Filedata.ToArray(), 0, attachment.Filedata.Length);
Response.End();

(my attachments are actually stored in a database table in this case, but it's just writing a byte array to the output stream)

rather than your approach ...

Response.TransmitFile(filepath);

The transfer speed seems pretty good. I've downloaded 3.5MB in a matter of seconds from the live website. (not just locally!)

I know I should be using a HttpHandler rather than hijacking the Response, but this works for now. Also, I should probably read the byte array in chunks to avoid taking up too much memory. I will go back at some point and rework it a little.

So, you could try using Response.OutputStream.Write or writing a HttpHandler instead.

Anyway, I hope that helps you out.

感受沵的脚步 2024-08-27 14:25:59

Response.AddHeader("Content-Length", ...) 是一场灾难!

我们使用.NET 4.0并经历了很多奇怪的事情。随机下载损坏。我们缩小了范围,发现发送到客户端的响应标头中的内容长度存在差异。我们不知道为什么,可能是 .NET 4.0 的 bug?但是,一旦我们注释掉 Response.AddHeader("Content-Length", ...) 代码行,所有问题都消失了。

编辑:启用 IIS7 动态压缩时,内容长度的差异可能是不可避免的。

Response.AddHeader("Content-Length", ...) is a catastrophe!

We used .NET 4.0 and experienced plenty of weird & random download corruptions. We narrowed down to a discrepancy of the content length in the response header sent to the client. We don't know why, may be a .NET 4.0 bug? But as soon as we commented out the line of code Response.AddHeader("Content-Length", ...) all issues disappeared.

EDIT: may be discrepancy in content-length is unavoidable when IIS7 Dynamic Compression is enabled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文