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...