Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Setting Content Type of a azure blob

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 714
    Comment on it

    When we upload a file on azure blob in byte array format or in any other format then by default it is uploaded as a content type of stream. As a result of it, when we try to get that file from a browser it always starts downloading the file. It happens because we had kept the content type as stream(By default). Several times we keep a HTML page inside a blob and when we try to access that page, it starts downloading.

    To overcome this problem, we should set the correct content-type of blob while writing the file on blob. Here is an example.

     public void UploadBlob(string containerName, string blobName, byte[] content)
            {
    
                {
                    StorageCredentialsAccountAndKey creds = new StorageCredentialsAccountAndKey(BlobAccountName, BlobAccountKey);
                    CloudStorageAccount account = new CloudStorageAccount(creds, false);
                    CloudBlobClient blobStorage = new CloudBlobClient(BlobBaseUri, creds);
                    CloudBlobContainer container = blobStorage.GetContainerReference(BlobThemeAccountName);
                    CloudBlob blob = container.GetBlobReference(blobName);
    
                    if (blobName.ToUpper().Contains(".HTML"))
                        blob.Properties.ContentType = "text/html";
                    blob.UploadByteArray(content);                
                }
            }
    

    Above is a method that writes a given byte content on azure blob. I had to put a file in a public container and then had to access the file inside iframe using the url of blob.

    I uploaded the file without setting the content type and when I tried to access that file inside iframe, it started downloading. After that I had to set the content type to ".HTML" in order to access it from a browser. I set the content-type using above method and then it started working

    I hope this will help many of those who use azure for hosting their application and data.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: