我正在写文件拉链。
我无法计算未来存档的正确文件大小,因此可以指定 Content-Length。
在两个 Thread.Sleep() 方法返回之前,此代码不会提示保存文件对话框。
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();
}
}
在 Chrome 和 IE 10 中,20 秒后我得到了保存对话框......
这是一种解决方法吗?
更新:
菲德勒发疯了。不确定,也许 Fidler 会自动添加 Conent-Length。这是我从提琴手那里得到的原始数据
在开始时
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
下载完成后:
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
更新 2:
fiddler coused such veird behaviour, problem closed. Turn off your debug proxies bros...