流zip文件MVC.NET开始直播 [英] Stream zip file MVC.NET start streaming

查看:80
本文介绍了流zip文件MVC.NET开始直播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个不断流的zip文件作为用户下载的方法,它(所以没有浪费流)

I'm trying to create a method which continually streams a zip file as a user downloads it (so that there is no wasted streaming)

我添加了一个Thread.sleep代码来模拟延迟

I added a thread.sleep to simulate latency

public override void ExecuteResult(ControllerContext context) {
    HttpResponseBase response = context.HttpContext.Response;

    response.Clear();
    response.ClearContent();
    response.ClearHeaders();
    response.Cookies.Clear();
    response.ContentType = ContentType;
    response.ContentEncoding = Encoding.Default;
    response.AddHeader("Content-Type", ContentType);
    context.HttpContext.Response.AddHeader("Content-Disposition", 
                            String.Format("attachment; filename={0}", 
                            this.DownloadName));
    int ind = 0;
    using (ZipOutputStream zipOStream = 
                new ZipOutputStream(context.HttpContext.Response.OutputStream))
    {
        foreach (var file in FilesToZip)
        {
            ZipEntry entry = new ZipEntry(FilesToZipNames[ind++]);
            zipOStream.PutNextEntry(entry);
            Thread.Sleep(1000);
            zipOStream.Write(file, 0, file.Length);
            zipOStream.Flush();
        }
        zipOStream.Finish();
    }    
    response.OutputStream.Flush();
}

这似乎是拉链,直到所有文件的文件压缩将无法启动流。有没有一种方法不断流?也许用一个不同的库?

It seems like the zip will not start streaming until all the files are files are zipped. Is there a way to stream continuously? Maybe with a different library?

推荐答案

假设zip格式是偏流,你的问题是,你的反应是由默认缓冲。如果设置<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.htt$p$psponsebase.bufferoutput%28loband%29.aspx\">Htt$p$psponseBase.BufferOutput为假,就应该立即开始流。

Assuming the zip format is partial to streaming, your problem is that your response is being buffered by default. If you set HttpResponseBase.BufferOutput to false, it should start streaming immediately.

这篇关于流zip文件MVC.NET开始直播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆