I have a controller with two pages, Index and Download, when clicking download it retrieves a byte[] from the service and I use Response.BinaryWrite to save the file. The problem with this is that when using Response.Clear it stops the Download page from rendering but downloads the file successfully. Is there any way to download the file and render the page?
I'm using .NET 4, ASP, Monorail Castle, C#
I am aware that by using MVC I am able to use ActionResult and FileResult as return types for my views, I am however limited to using Monorail Castle due to it being an existing project I've taken ownership of recently and have no weight in changing it's technology just yet.
Below is my example code
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);
}
}
Here is the Index page
${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()}
Here is the download page
<table>
<tr>
<td>Your file has been downloaded successfully</td>
</tr>
</table>