【问题标题】:How to Download Azure blob file into download folder, not in browser如何将 Azure blob 文件下载到下载文件夹中,而不是在浏览器中
【发布时间】:2020-07-24 20:10:02
【问题描述】:
【问题讨论】:
标签:
azure
azure-blob-storage
azure-sas
【解决方案1】:
您需要确保Content-Type 和Content-Disposition 标头具有触发浏览器下载文件的值。尤其是 Content-Disposition 很重要。
Content-Type: application/octet-stream
Content-Disposition: attachment
您可以设置内容处置on the blob itself
var blob = container.GetBlobReference(userFileName);
blob.Properties.ContentDisposition = "attachment";
blob.SetProperties();
或add it to your SAS token(另见相应的blog post)。
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("videos");
string userFileName = service.FirstName + service.LastName + "Video.mp4";
CloudBlockBlob blob = container.GetBlockBlobReference(userFileName);
SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy()
{
Permissions = SharedAccessBlobPermissions.Read,
SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1)
};
SharedAccessBlobHeaders blobHeaders = new SharedAccessBlobHeaders()
{
ContentDisposition = "attachment; filename=" + userFileName
};
string sasToken = blob.GetSharedAccessSignature(policy, blobHeaders);
var sasUrl = blob.Uri.AbsoluteUri + sasToken;//This is the URL you will use. It will force the user to download the video.