【发布时间】:2023-11-13 22:03:01
【问题描述】:
public ActionResult Index(int id, string name)
{
var model = new ITViewModel
{
Packages = _Repository.GetDeployedPackages(id)
};
return View(model);
}
[HttpPost]
public ActionResult GeneratePackage(ITViewModel model)
{
_Repository.SavePackage(model);
//Generate Zip file Package
//Get file template in archiveStream
Response.Clear();
Response.ContentType = "application/zip";
Response.AppendHeader("content-disposition", "attachment; filename="testzipPackage");
Response.CacheControl = "Private";
Response.Cache.SetExpires(DateTime.Now.AddMinutes(3));
Response.Buffer = true;
var writeBuffer = new byte[4096];
var count = archiveStream.Read(writeBuffer, 0, writeBuffer.Length);
while (count > 0)
{
Response.OutputStream.Write(writeBuffer, 0, count);
count = archiveStream.Read(writeBuffer, 0, writeBuffer.Length);
}
model.Packages = _Repository.GetDeployedPackages(model.id) //get the correct package list with the one tht we just saved on this ActionResult
return View("Index",model);
}
//Index
@model ITViewModel
@using (Html.BeginForm("GeneratePackage", "Integration", FormMethod.Post)
{
//some input form
}
<table>
@foreach (var package in Model.Packages)
{
<tr>
<td>
@package.Name
</td>
</tr>
}
</table>
我能够正确下载 zip 文件。在调试器中,我还看到了带有新添加元素的包列表。但是帖子上的视图没有刷新。我的意思是 Index 上的表格不会用新的模型元素刷新。甚至 document.ready 也没有被调用一次 return View("Index",model) 被触发。
I have tried ModelState.Clear(). It didn't work.
【问题讨论】:
-
你不能让一个 HTTP 响应同时做两件事。内容是
text/html(用于查看)或application/zip(用于下载),但不能两者兼有。您将需要执行其他操作,例如返回视图,然后通过 javascript 开始下载。 -
@PaulAbbott wat 是解决这个问题的方法吗?我需要用户下载 zip。但我也希望他们看到更新后的列表。我可以在压缩 zip 后强制刷新页面吗
-
解决方法是返回视图,然后通过 javascript 开始下载。您将需要进行第二个控制器操作来下载文件并通过 javascript 调用它。你不能先下载再刷新页面,因为下载只是一个文件传输,下载完成后没有办法包含“指令”来做任何其他事情。
标签: c# asp.net asp.net-mvc httpresponse