您需要确保Content-Type
和Content-Disposition
标头具有触发浏览器下载文件的值。尤其是内容处置很重要。
Content-Type: application/octet-stream
Content-Disposition: attachment
您可以在 blob 本身上设置内容处置
var blob = container.GetBlobReference(userFileName);
blob.Properties.ContentDisposition = "attachment";
blob.SetProperties();
或将其添加到您的 SAS 令牌中(另请参阅相应的博客文章)。
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.