1

I am writing on the fly file zipper.

I cant calculate correct file size of a future archive, so can specify Content-Length.

This code did not prompt save file dialog until both Thread.Sleep() methods returned.

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Download()
    {
        return new DelayedUnsepcifiedLengthZipArchiveResult();
    }
}

public class DelayedUnsepcifiedLengthZipArchiveResult : ActionResult
{
    public override void ExecuteResult(ControllerContext context)
    {
        context.HttpContext.Response.ContentType = "application/zip";
        context.HttpContext.Response.CacheControl = "private";
        context.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        context.HttpContext.Response.AddHeader("content-disposition", string.Format("attachment; filename=\"{0}\"", "test.zip"));
        context.HttpContext.Response.Flush();

        Thread.Sleep(10000);

        context.HttpContext.Response.Write("hello world");
        context.HttpContext.Response.Flush();

        Thread.Sleep(10000);

        context.HttpContext.Response.Write("hello world2");
        context.HttpContext.Response.Flush();

        context.HttpContext.Response.End();
    }
}

In Chrome and IE 10 I got save dialog after 20 secs...

Is it a way to fix it?

Update:

Fidler goes crazy. Not sure, maybe Fidler adds Conent-Length automatically. This is raw data I getting from fidler

At the start

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Transfer-Encoding: chunked
Content-Type: application/zip
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNetMvc-Version: 4.0
content-disposition: attachment; filename="test.zip"
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpcV29ya1xBY2FkZW15XEhpZ2hMb2FkQ2hhblxEb3dubG9hZGVyXGhvbWVcZG93bmxvYWQ=?=
X-Powered-By: ASP.NET
Date: Mon, 04 Mar 2013 13:39:24 GMT

After download finished:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/zip
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNetMvc-Version: 4.0
content-disposition: attachment; filename="test.zip"
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpcV29ya1xBY2FkZW15XEhpZ2hMb2FkQ2hhblxEb3dubG9hZGVyXGhvbWVcZG93bmxvYWQ=?=
X-Powered-By: ASP.NET
Date: Mon, 04 Mar 2013 13:39:24 GMT
Content-Length: 23

hello worldhello world2

UPDATE 2:

fiddler coused such veird behaviour, problem closed. Turn off your debug proxies bros...
v00d00
  • 3,215
  • 3
  • 32
  • 43
  • 1
    what happens if you put a context.HttpContext.Response.Write("hello world 0"); before your first Response.Flush(); – jbl Mar 04 '13 at 13:42
  • 1
    see http://stackoverflow.com/questions/3702448/using-a-memorystream-with-filestreamresult-possible you could possibly use `FileStreamResult` as it takes a `Stream` as a ctor arg – wal Mar 04 '13 at 13:48
  • @jbl Same behaviour. But after I turned off Fiddler, chrome started to add a file to the list of downloads. – v00d00 Mar 04 '13 at 13:54
  • @wal if I will use FileStreamResult I will have to feed it a memmory stream filled with a data. But in current implementation I can use single download stream and send data to client zipped, just after I got a new chunk. – v00d00 Mar 04 '13 at 13:54
  • it takes a `Stream` not just a `MemoryStream` - i'm not saying it would work but it would compile. you would have to try it. Also, its going to be grossly inefficient to zip things 'on the fly' - sure its convenient the 1st time around but subsequent requests for the same file are going to result in unnecessary zipping – wal Mar 04 '13 at 13:57
  • 1
    Seems like a problem with Fiddler - Chrome 25 with no Fiddler shows SaveFile dialog immediately. – Miroslav Bajtoš Mar 04 '13 at 14:36

0 Answers0