Download 使用带有.NET Web Api 2的Angular2将文件从Azure存储下载到客户端

Download 使用带有.NET Web Api 2的Angular2将文件从Azure存储下载到客户端,download,stream,azure-storage-blobs,Download,Stream,Azure Storage Blobs,我正在尝试将1GB文件从blob存储下载到客户端。我使用了之前的内存流,我得到了OutOfMemory异常 现在,我试图从blob中打开一个读取流,并将其直接发送到客户端 [HttpGet] [ResponseType(typeof(HttpResponseMessage))] public async Task<HttpResponseMessage> DownloadAsync(string file) { HttpResponseMessage result

我正在尝试将1GB文件从blob存储下载到客户端。我使用了之前的内存流,我得到了OutOfMemory异常

现在,我试图从blob中打开一个读取流,并将其直接发送到客户端

 [HttpGet]
 [ResponseType(typeof(HttpResponseMessage))]
 public async Task<HttpResponseMessage> DownloadAsync(string file)
 {
     HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
     var stream = await blob.OpenReadAsync("container", file);
     result.Content = new StreamContent(stream);
     return result;
 }
有人知道怎么修吗


谢谢大家!

要修复它,您需要使用以下javascript代码:

var fileUri = "http://localhost:56676/api/blobfile";  //replace with your web api endpoint
var link = document.createElement('a');
document.body.appendChild(link);
link.href = fileUri;
link.click();
然后在后端,这样做:

HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
var stream = await blob.OpenReadAsync("container", file);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
    FileName = "data.zip"
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return result;

我也有同样的问题

我想出的解决办法是——

首先,只有当客户端尝试从blob下载文件时,才会出现预期的行为,通常我更喜欢从客户端本身下载文件

和您的情况一样,尝试获取文件blob uri,并执行以下操作,以使用Angular Router或仅使用window.location.href在浏览器中打开文件

window.location.href=“https://*/filename.xlsx”


这对我有用

不幸的是,它没有解决问题。下载仍不会显示在浏览器下载中。但当Google Chrome的网络标签打开时,它会在请求中运行。它对我很有用。测试浏览器缓存时是否清除了缓存?找到解决方案了吗?@Neel很遗憾,没有!如果您找到了问题的解决方案,请随意添加它作为答案,以便其他开发人员可以从中学习。谢谢大家!@SamySammour我也有类似的问题。你知道怎么解决吗?@ark不,我是直接从Blob存储而不是服务器下载的
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
var stream = await blob.OpenReadAsync("container", file);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
    FileName = "data.zip"
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return result;