我有一個控制器有兩個頁面,索引和下載,當點擊下載它從服務中檢索一個字節[],我用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