Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在开始发送之前,HttpResponse.Filter会缓冲整个数据吗?_C#_.net_Asp.net_Compression_Httpresponse - Fatal编程技术网

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

C# 在开始发送之前,HttpResponse.Filter会缓冲整个数据吗?,c#,.net,asp.net,compression,httpresponse,C#,.net,Asp.net,Compression,Httpresponse,一位用户发布了这篇文章。但是如果我尝试传输4G文件会发生什么?它会将整个文件加载到内存中以进行压缩吗?否则它会一块一块地压缩它 我是说,我现在正在做这件事: public void GetFile(HttpResponse response) { String fileName = "example.iso"; response.ClearHeaders(); response.ClearContent();

一位用户发布了这篇文章。但是如果我尝试传输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是否做同样的事情,否则它会将整个文件加载到内存中以压缩它

还有,我对此有点不安全。。。可能需要在内存中加载整个文件来压缩它。。。是吗


干杯。

HttpResponse.Filter是一个流:您可以分块写入它


你做得对。您正在使用FileStream和DeflateStream从文件中读取并压缩它。您每次读取4096字节,然后将它们写入响应流。因此,您所使用的只是4096字节(还有一点)的内存。

我知道我可以分块编写,但我的问题是。。。它会一块一块地发送吗?或者,它会等待加载整个流来压缩它,然后发送它吗?那么它就不是“流”。当您在响应流中写入时,响应流被传输到目标。它将只在通过网络发送时保存在内存中。Deflate stream不会读取整个文件,因为它正在使用FileStream以块(4096字节)的形式从文件中读取。