Fixing Azure Blob Storage Content Disposition
The latest release of the Windows Azure Blob Storage API and Client Library supports the content-disposition header allowing you to force a file to download rather than display it in the browser. It also allows you to override the blob filename and specify a friendly download name. This can be very useful but you may find that it doesn't work out of the box. This article explains how to get it working.
Setting the content disposition header
Setting the content disposition header using the Azure Blob Storage Client Library is very simple. You only need to add one line to your existing upload code:
blob.Properties.ContentDisposition = "attachment; filename=" + downloadName;
Depending on the age of your storage account, this may be all that is required but if you find that the header is not having any effect, read on. If your file is uploading without error but when you come to download the file, it looks as though the content-disposition header is being ignored, then there is a good chance that you have an empty or out of date DefaultServiceVersion.
Setting the DefaultServiceVersion
What the heck is a DefaultServiceVersion you ask. Well you can read more about versioning on MSDN but suffice to say that it is required if you want to get ContentDisposition working.
Setting the DefaultServiceVersion is relatively simple. Just execute the following code:
var blobClient = storageAccount.CreateCloudBlobClient(); var serviceProperties = blobClient.GetServiceProperties(); serviceProperties.DefaultServiceVersion = "2013-08-15"; blobClient.SetServiceProperties(serviceProperties);
As with enabling CORS support, you only need to do this once.
This info came from a stackoverflow answer by Gaurav Mantri so a big thank you to him as there is very little information out there at present.
Useful or Interesting?
If you liked the article, I would really appreciate it if you could share it with your Twitter followers.Share on Twitter
Comments are now closed for this article.