Blob Storage: Don’t forget the MIME

Invalid MIME Type

Invalid MIME Type

So a while back I was running into a strange issue with Azure. I was using blob storage to store images and videos and all was working fine. Working fine until I tried in IE that is. I started having flashbacks of IE introduced rage, misaligned pixels, broken mice, javascript error popups…

Then I noticed the jpgs were trying to download on iPhone, something wasn’t right images should just open in the browser. Then I checked the IE error console, and saw this message when trying to play video.

“Error: Unsupported video type or invalid file path”

However, I soon realized this one was on me, and Chrome just happens to gracefully handled my critical error. When you upload to blob storage, you must specify a MIME type. I have been working with html and only on rare occasions does the server not happen to properly handle MIME types. I honestly didn’t even know what MIME stood for. According to Wikipedia:

The identifiers were originally defined in RFC 2046, and were called MIME types because they referred to the non-ASCII parts of email messages that were composed using the MIME (Multipurpose Internet Mail Extensions) specification. They are also sometimes referred to as Content-types.

Realizing my mistake, I wrote a ‘scrub’ program that went through all of my Azure blobs and set their content-type type based on file extension. I then modified my ‘blob manager’ code to properly set the content type when creating new items and all was right with my project.

You can see, I had to manually set each file extension and manually set the type. I figured there had to be a better way.

public static void ResetBlobs(string containerName)
	var client = _GetBlobClient();
	CloudBlobContainer container = client.GetContainerReference(containerName);
	foreach (var blob in container.ListBlobs().OfType())
		string extension = Path.GetExtension(blob.Uri.AbsoluteUri);
		switch (extension)
			case ".mp4":
				blob.Properties.ContentType = "video/mp4";
			case ".jpg":
			case ".jpeg":
				blob.Properties.ContentType = "image/jpeg";
				// Add in all MIME Types

After further inspection, I found that .NET 4.5 has a method MimeMapping.GetMimeMapping that gets MIME mapping based on the file name. This saves the time of putting together a dictionary to map all extensions to the proper mime types.

However, when running my program with the updated code, I noticed ‘.mp4’ files were being returned as ‘application/octet-stream’ instead of ‘video/mp4’ so I had to revert back to my case block. After some google-fu I found a relevent stack exchange and just grabbed a mime dictionary from a similar post, as I didn’t have the motivation to put together my own comprehensive dictionary.

Lesson learned: never forget your mime.

Leave a Reply

Your email address will not be published. Required fields are marked *