ASP.NET MVC:下载流而不冻结

时间:2013-09-09 14:21:07

标签: c# wcf asp.net-mvc-4 azure-storage-blobs

我们使用ASP.NET MVC 4(演示文稿),WCF(逻辑),Azure Blob存储(存储)。

是否可以在不冻结网站导航的情况下实施流下载(传递)?

我们确实需要“传递”,因为需要自定义标头(Content-Disposition等)。这意味着无法使用FilePathResult和指向Azure的直接链接。

现在通过这种方式实现下载:

[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();
} 

2 个答案:

答案 0 :(得分:0)

也许你的按钮应该在控制器中对你的web方法进行javascript ajax调用..

答案 1 :(得分:0)

通过在存储抽象和浏览器之间添加代理WCF休息文件服务来解决。