简体   繁体   English

带重命名的 ASP.NET Core AzureBlob 下载文件

[英]ASP.NET Core AzureBlob download file with rename

I have the following code on my page:我的页面上有以下代码:

<a class="btn-default btn" href="<AzureBlobContainerUrl>/<guid>.psd" download="@Model.FileName" target="_blank">Download 
    <span class="glyphicon glyphicon-export"></span>
</a>

@Model.FileName represents eg 'MyPhotoshopFile.psd' @Model.FileName 代表例如“MyPhotoshopFile.psd”

First Problem: The downloaded file has the original GUID as filename not eg 'MyPhotoshopFile.psd'.第一个问题:下载的文件使用原始 GUID 作为文件名,而不是例如“MyPhotoshopFile.psd”。

Second Problem: If the file is a eg PDF then Google opens the file directly in the current browser tab instead of downloading it.第二个问题:如果文件是例如 PDF,那么 Google 会直接在当前浏览器选项卡中打开该文件,而不是下载它。

Any solutions for that?任何解决方案?

For the first problem, try setting the Content-Disposition property on the blob.对于第一个问题,请尝试在 blob 上设置 Content-Disposition 属性。 If you're using the C# SDK, then grab the blob and you can set the CD using something like this:如果您使用的是 C# SDK,则获取 blob,您可以使用以下内容设置 CD:

blob.FetchAttributes();
blob.Properties.ContentDisposition = string.Format("attachment;filename=\"{0}\"", yourFileName);
blob.SetProperties();

Based on this older question ( Chrome Download Attribute not working ), the "download" attribute in Chrome behaves differently based on the CD, so setting this explicitly will likely solve your second problem.基于这个较旧的问题( Chrome 下载属性不起作用), Chrome 中的“下载”属性根据 CD 的行为不同,因此明确设置它可能会解决您的第二个问题。

Well, thank you very much for your tips!嗯,非常感谢你的提示! First, the code and your advice weren't working (the CD attribute was ignored by azure blos storage services), but after some hours of research I identified the issue:首先,代码和您的建议不起作用(azure blos 存储服务忽略了 CD 属性),但经过几个小时的研究,我发现了问题:

  • AccountKind StorageV2 (general purpose v2) is NOT working with the CD attribute AccountKind StorageV2(通用V2)与CD属性工作
  • AccountKind BlobStorage is working without any problems AccountKind BlobStorage运行没有任何问题

I recreated the blob storage and used the legacy BlobStorage account type.我重新创建了 Blob 存储并使用了旧的 BlobStorage 帐户类型。 With this the ContentDisposition Attribute is working.有了这个 ContentDisposition 属性正在工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM