要下载的 HTML 链接:
<a asp-action="DownloadFile" asp-route-id="@Model.ID">@Html.DisplayFor(model => model.BlobName)</a>
Generated HTML link: http://localhost:50325/DebitMemos/DownloadFile/76
对应的 Controller :
public async Task<IActionResult> DownloadFile(int id)
{
var debitMemo = await _context.DebitMemo
.SingleOrDefaultAsync(m => m.ID == id);
CloudBlockBlob blockBlob;
MemoryStream ms = new MemoryStream();
try
{
CloudBlobContainer container = DebitMemo.GetAzureContainer();
blockBlob = container.GetBlockBlobReference(debitMemo.BlobName);
await container.CreateIfNotExistsAsync();
// Save blob contents to a file.
await blockBlob.DownloadToStreamAsync(ms);
Stream blobStream = await blockBlob.OpenReadAsync();
return File(blobStream, blockBlob.Properties.ContentType, debitMemo.BlobName);
}
catch (StorageException)
{
return Content("File does not exist");
}
}
点击 Chrome 中的链接,提示输入文件下载位置,然后成功下载。
另一方面,Internet Explorer(我需要应用程序在其中运行)说“你想从本地主机打开还是保存 76 (1.54 KB)?”
该示例中的“76”是 debitMemo 的 ID,我不知道它为什么这样调用它而不是 blobName。然后,当我单击“保存”时,它只显示“无法下载 76”。没有其他解释。
如果我需要提供任何其他信息,请告诉我。
最佳答案
不要使用内存流。如果这样做,您将不得不管理该内存流。
相反,只需打开到 blob 的流并将其连接到 ASP.NET 的响应流。
var stream = await blockBlob.OpenReadAsync();
return File(stream, blockBlob.Properties.ContentType, debitMemo.BlobName);
关于c# - .NET Core 2 MVC - 从 Azure Blob 存储下载文件在 Google Chrome 中有效,但在 Internet Explorer 中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49121169/