Monday, 9 September 2013

ASP.NET MVC: Download stream without freezing

ASP.NET MVC: Download stream without freezing

We use ASP.NET MVC 4 (Presentation), WCF (Logic), Azure Blob Storage
(Storage).
Is it possible to implement stream-downloading (pass through) without
freezing navigation on web-site?
We exactly need "passing through" because custom headers are required
(Content-Disposition etc). It means that FilePathResult and direct link to
Azure are not possible.
Downloading is implemented by this way now:
[HttpGet]
public ActionResult DownloadTemplate(Guid templateId)
{
Response.Clear(); Response.BufferOutput = false;
DownloadResult result = Client.DownloadTemplate(templateId)
Response.AddHeader("Content-Type",
MimeHelper.GetMimeType(result.FileName));
Response.AddHeader("Content-Disposition", "attachment; filename=" +
result.FileName);
byte[] buffer = new byte[4096]; int readed = 0;
while ((readed = result.ContentStream.Read(buffer, 0, buffer.Length))
> 0)
{
if (Response.IsClientConnected)
{
Response.OutputStream.Write(buffer, 0, readed);
Response.Flush();
}
}
return new EmptyResult();
}

No comments:

Post a Comment