HttpResponse.Filter 会在开始发送之前缓冲整个数据吗?

发布于 2024-08-28 04:45:54 字数 1350 浏览 3 评论 0原文

用户发布了这篇关于如何使用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 技术交流群。

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

发布评论

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

评论(1

鲸落 2024-09-04 04:45:54

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.

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