流zip文件MVC.NET开始流式传输

我正在尝试创建一种方法,在用户下载时不断地传输zip文件(这样就不会浪费流媒体)

我添加了一个thread.sleep来模拟延迟

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

看起来拉链不会开始流式传输,直到所有文件都是压缩文件。 有没有办法连续流? 也许有一个不同的图书馆?

假设zip格式部分流式传输,您的问题是默认情况下您的响应正在缓冲。 如果将HttpResponseBase.BufferOutput设置为false,则应立即开始流式传输。

我想你可能需要刷新输出流而不是zipostream来查看任何输出到http。 所以在这种情况下,response.OutputStream.Flush()会出现在你的循环中。 不确定它是否真的可以解决你的问题。