我有一个控制器有两个页面,索引和下载,当点击下载它从服务中检索一个字节[],我用Response.BinaryWrite保存文件。与此问题是,使用Response.Clear时,它会停止呈现下载页面,但成功下载文件。有什么方法可以下载文件并渲染页面?如何:从web服务保存文件,而不停止页面呈现
我使用.NET 4,ASP,单轨城堡,C#
我意识到,通过使用MVC我能够使用的ActionResult和FileResult作为返回类型我的看法,我不过限于使用单轨城堡是因为它是我最近拥有的一个现有项目,在改变它的技术方面没有任何重量。
下面是我的示例代码
public class MyController : Controller
{
public void Index()
{
PropertyBag["includeZeroBalances"] = false;
PropertyBag["toDate"] = DateTime.Today.ToShortDateString();
}
public void Download(bool includeZeroBalances, DateTime toDate)
{
MyProxy proxy = GetProxy();
byte[] file = proxy.GetFile(includeZeroBalance, toDate);
Response.Clear();
Response.ContentType = "application/zip";
Response.AppendHeader("Content-Disposition", "attachment; filename="TestFileName.zip");
Response.BinaryWrite(file);
}
}
这里是索引页
${Form.FormTag({@action: 'Download'})}
<table>
<tr>
<td>${Form.LabelFor("toDate", "To Date (yyyy/mm/dd):")}</td>
<td><input type="text" id="toDate" name="toDate" value="${?toDate}" /></td>
</tr>
<tr>
<td>${Form.LabelFor("includeZeroBalances", "Include Zero Balances:")}</td>
<td>${Form.CheckboxField("includeZeroBalances")}</td>
</tr>
<tr>
<td> </td>
<td>${Form.Submit("Download", {@class: 'submitb'})}</td>
</tr>
</table>
${Form.EndFormTag()}
这里是下载页面
<table>
<tr>
<td>Your file has been downloaded successfully</td>
</tr>
</table>
嗨Mauricio,谢谢你提供的链接和见解。不幸的是,这并不完全是我所期待的。 – 2012-01-10 14:18:07
@ROFFELPOMP:恩,这正是你在答案中所做的。 – 2012-01-10 14:39:30