Reputation: 8147
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?
Upvotes: 44
Views: 43822
Reputation: 3843
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);
Upvotes: 23
Reputation: 837
Using the new SDK Azure.Storage.Blobs
BlobHttpHeaders blobHttpHeaders = new BlobHttpHeaders();
blobHttpHeaders.ContentType = "image/jpg";
blobClient.SetHttpHeaders(blobHttpHeaders);
Upvotes: 22
Reputation: 5257
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();
Upvotes: 3
Reputation: 955
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();
Upvotes: 1
Reputation: 136216
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.
Upvotes: 116
Reputation: 11319
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.
Upvotes: 22