【发布时间】:2023-03-27 08:40:02
【问题描述】:
我有一个控制器,它有两个页面,索引和下载,当单击下载时,它会从服务中检索一个字节 [],然后我使用 Response.BinaryWrite 来保存文件。这样做的问题是,当使用 Response.Clear 时,它会停止渲染下载页面,但会成功下载文件。有没有办法下载文件并渲染页面?
我正在使用 .NET 4、ASP、Monorail Castle、C#
我知道通过使用 MVC,我可以使用 ActionResult 和 FileResult 作为我的视图的返回类型,但是我只能使用 Monorail Castle,因为它是我最近拥有的一个现有项目并且没有改变它的技术的重要性。
下面是我的示例代码
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>
【问题讨论】:
标签: c# asp.net download castle-monorail