44

I'm using Microsoft.WindowsAzure.Storage.* library from C#.

This is how I'm uploading things to storage:

// Store in storage
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("...connection string...");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("pictures");

// Create container if it doesnt exist
container.CreateIfNotExists();

// Make available to everyone
container.SetPermissions(new BlobContainerPermissions
{
    PublicAccess = BlobContainerPublicAccessType.Blob
});

// Save image
CloudBlockBlob blockBlob = container.GetBlockBlobReference("blah.jpg");
blockBlob.UploadFromByteArray(byteArrayThumbnail, 0, byteArrayThumbnail.Length);
blockBlob.Properties.ContentType = "image/jpg";  // *** NOT WORKING ***

All the things I upload to the storage are being saved with content type "application/octet-stream", even though I'm using the setter with value "image/jpg" (see the last line in my code).

So question #1: Why isn't working the ContentType setter?

And question #2: If I manually change the content type to "image/jpg", using Windows Azure management portal, and then copy the absolute URI of the file to the browser's address field, and press enter, the jpg file is downloaded instead of displayed. Isn't this mime type supposed to be displayed instead of downloaded? How do I change this?

6 Answers 6

116
Answer recommended by Microsoft Azure Collective

Actually you don't have to call SetProperties method. In order to set content type while uploading the blob, just set the ContentType property before calling the upload method. So your code should be:

// Save image
CloudBlockBlob blockBlob = container.GetBlockBlobReference("blah.jpg");
blockBlob.Properties.ContentType = "image/jpg";
blockBlob.UploadFromByteArray(byteArrayThumbnail, 0, byteArrayThumbnail.Length);

and that should do the trick.

8
  • Whoa!, I'd accept both answers :/ anyway, no one answered my second question: when I put the URL in the browser, it downloads the image as a file instead of being displayed. Tried image/jpeg and image/jpg.
    – sports
    Commented Jul 8, 2014 at 3:44
  • Actually I tried uploading a JPG file and set its content type to be image/jpg. When I put the URL in the browser, I was able to see the image in the browser. I tried with IE/Firefox/Chrome. Can you please trace the request/response in Fiddler and see the actual content type returned by storage service? Commented Jul 8, 2014 at 3:48
  • You uploaded via SDK or directly from the Azure Portal? I will try the trace
    – sports
    Commented Jul 8, 2014 at 4:49
  • 2
    Actually that's not true, I believe you may be seeing this behavior because of the file name having an extension on it. If you use some other identifier for the name of the blob (like a database key) or use a name that does not have an extension, you will not get this behavior. Commented Sep 16, 2016 at 18:00
  • 1
    This does not set the content-type. It might have worked for you because of the jpg extension in the blob name. Commented Aug 12, 2017 at 18:42
23

Unfortunately, none of the answers provided here is currently working for the latest SDK (12.x.+)

With the latest SDK, the content type should be set via BlobHttpHeaders.

var _blobServiceClient = new BlobServiceClient("YOURCONNECTIONSTRING");

var containerClient = _blobServiceClient.GetBlobContainerClient("YOURCONTAINERNAME");

var blob = containerClient.GetBlobClient("YOURFILE.png");

var blobHttpHeader = new BlobHttpHeaders();

blobHttpHeader.ContentType = "image/png";

var uploadedBlob = await blob.UploadAsync(YOURSTREAM, blobHttpHeader);
2
  • The perfect solution for people who are working with latest Azure Blob SDKs (v12 and above). Thanks!
    – Yash Gupta
    Commented Jan 7, 2021 at 10:39
  • 1
    How can you combine this with overwrite any existing file? edit: actually looks like you don't need it when you add these options.
    – RoLYroLLs
    Commented Oct 16, 2023 at 22:32
22

After you make any changes to Properties, you have to make a call to CloudBlockBlob.SetProperties() to actually save those changes.

Think of it as something similar to LINQ-to-Entities. You can make any changes you want to your local object, but until you call SaveChanges(), nothing is actually saved.

2
  • 3
    The SetProperties method should be used for UPDATING blobs not while CREATING.
    – AntonB
    Commented Jan 5, 2016 at 15:34
  • 2
    SetProperties should be used for any time, regardless if it's the first time create or update. From my observation, if you use a filename (with extension) then it defaults the ContentType to something that matches the extension. If you want to ensure it always sets it to the content type you expect, I'd (1) uploading the data, (2) set your content tye (3) call SetProperties() azure.microsoft.com/en-us/documentation/articles/… Commented Sep 16, 2016 at 18:03
22

Using the new SDK Azure.Storage.Blobs

BlobHttpHeaders blobHttpHeaders = new BlobHttpHeaders();
blobHttpHeaders.ContentType = "image/jpg";
blobClient.SetHttpHeaders(blobHttpHeaders);
1
  • 4
    It crashes with BlockBlobClient.UploadAsync(Stream, BlobUploadOptions, CancellationToken) if the blob contains one piece only: Status: 404 (The specified blob does not exist.) The correct way is to set BlobUploadOptions.Headers before calling UploadAsync. Commented Sep 30, 2020 at 5:30
3

Obviously best to set it on create like Gaurav Mantri's answer, if you are past that point and need to update the other answers here may mess you up.

// GET blob
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName); 

// if you don't do this you'll wipe properties you didn't mean to
await blockBlob.FetchAttributesAsync();

// SET
blockBlob.Properties.ContentType = mimetype;

// SAVE
await blockBlob.SetPropertiesAsync();
1

with the new version of the Azure Blob SDK this is no longer working.

this worked for me:

CloudBlockBlob blockBlob = cloudBlobContainer.GetBlockBlobReference(blobName);
blockBlob.Properties.ContentType = contentType;                            
await blockBlob.SetPropertiesAsync();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.