HttpResponse.Filter 会在开始发送之前缓冲整个数据吗?
用户发布了这篇关于如何使用HttpResponse.Filter来压缩大量数据。但是如果我尝试传输 4G 文件会发生什么情况?它会将整个文件加载到内存中以进行压缩吗?否则它会逐块压缩它?
我的意思是,我现在正在这样做:
public void GetFile(HttpResponse response)
{
String fileName = "example.iso";
response.ClearHeaders();
response.ClearContent();
response.ContentType = "application/octet-stream";
response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
response.AppendHeader("Content-Length", new FileInfo(fileName).Length.ToString());
using (FileStream fs = new FileStream(Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data"), fileName), FileMode.Open))
using (DeflateStream ds = new DeflateStream(fs,CompressionMode.Compress))
{
Byte[] buffer = new Byte[4096];
Int32 readed = 0;
while ((readed = ds.Read(buffer, 0, buffer.Length)) > 0)
{
response.OutputStream.Write(buffer, 0, readed);
response.Flush();
}
}
}
所以在我阅读的同时,我正在压缩并发送它。然后我想知道 HttpResponse.Filter 是否做同样的事情,否则它会将整个文件加载到内存中以压缩它。
另外,我对此有点不安全......也许需要将整个文件加载到内存中来压缩它......是吗?
干杯。
An user posts this article about how to use HttpResponse.Filter to compress large amounts of data. But what will happen if I try to transfer a 4G file? will it load the whole file in memory in order to compress it? or otherwise it will compress it chunk by chunk?
I mean, I'm doing this right now:
public void GetFile(HttpResponse response)
{
String fileName = "example.iso";
response.ClearHeaders();
response.ClearContent();
response.ContentType = "application/octet-stream";
response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
response.AppendHeader("Content-Length", new FileInfo(fileName).Length.ToString());
using (FileStream fs = new FileStream(Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data"), fileName), FileMode.Open))
using (DeflateStream ds = new DeflateStream(fs,CompressionMode.Compress))
{
Byte[] buffer = new Byte[4096];
Int32 readed = 0;
while ((readed = ds.Read(buffer, 0, buffer.Length)) > 0)
{
response.OutputStream.Write(buffer, 0, readed);
response.Flush();
}
}
}
So at the same time I'm reading, I'm compressing and sending it. Then I wanna know if HttpResponse.Filter do the same thing, or otherwise it will load the whole file in memory in order to compress it.
Also, I'm a little bit insecure about this... maybe is needed to load the whole file in memory to compress it... is it?
Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
HttpResponse.Filter 是一个流:您可以在其中写入块。
你做得正确。您正在使用 FileStream 和 DeflateStream 读取文件并压缩它。您一次读取 4096 个字节,然后将它们写入响应流。所以您使用的只是 4096 字节(以及更多一点)的内存。
HttpResponse.Filter is a stream: You can write in it in chunks.
You are doing it correctly. You are using FileStream and DeflateStream to read from file and compress it. You are reading 4096 bytes at time and then writing them into the Response Stream. So all you are using is 4096 byte(and a little bit more) of memory.