1

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!

4

2 回答 2

1

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

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

传输速度似乎相当不错。我已经在几秒钟内从现场网站下载了 3.5MB。(不仅仅是本地!)

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

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

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

于 2010-02-18T22:16:44.977 回答
0

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

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

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

于 2011-05-13T14:56:31.767 回答