Azure Blob always downloads when navigating to url

Viewed 15476

In our application we give the user the abilty to upload a document to a windows azure blob storage account. After uploading the document or image it gets assigned some url (https://name.blob.core.windows.net/container/file-name.jpg). If the document is an image or a pdf or some file that can be rendered by the browser we are trying to display it in the browser without requiring the user to download the file. If we just open up a new window or tab and direct the user to the blob uri in IE, the image or pdf renders correctly in the browser. But, if we try to just open a new window pointing to the uri in Chrome, FireFox, or Safari, it just downloads the file instead of displaying it in the browser.

Is there a way to force the latter three browsers to just display the file instead of download it?

6 Answers

If using the Azure SDK (12.x+) there is a change that requires the use of BlobHttpHeaders that pass in to the upload method (instead of blob.Properties.ContentType).

For example:

    var header = new BlobHttpHeaders();
    header.ContentType = "image/jpeg";

    var response = await blobClient.UploadAsync(stream, header);
public String sasURL(String filePath) throws URISyntaxException, StorageException, InvalidKeyException {
    CloudBlockBlob cloudBlockBlob = cloudBlobContainer.getBlockBlobReference(filePath);
    SharedAccessBlobPolicy sasPolicy = new SharedAccessBlobPolicy();
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.add(Calendar.HOUR, 3);
    sasPolicy.setSharedAccessExpiryTime(calendar.getTime());
    sasPolicy.setPermissions(EnumSet.of(SharedAccessBlobPermissions.READ, SharedAccessBlobPermissions.LIST));
    SharedAccessBlobHeaders sharedAccessBlobHeaders = new SharedAccessBlobHeaders();
    sharedAccessBlobHeaders.setContentType(MediaType.APPLICATION_PDF_VALUE);
    String sas = cloudBlockBlob.generateSharedAccessSignature(sasPolicy, sharedAccessBlobHeaders,null);
    return cloudBlockBlob.getUri()+"?"+sas;
}

Thanks to sudhAnsu63 and Steve G for helpful answers!

Here is a minimum reproducible code solution in visual basic that sets ContentType and uploads to azure blob storage by calling the function "uploadImageBlob", setting the content type will help the browser with knowing what to do with the image, so that it will just display the image/file instead of starting a download when accessed.

As an aside I would not just copy and paste this into your code and would hope you handle your connection strings and file names in a better more dynamic manner.

            Public Function uploadImageBlob() As String
                ' variables we will need in this function
                Const containerName As String = "exampleContainer"
                Dim azureConnectionString As String = WebConfigurationManager.ConnectionStrings("NameOfConnectionString").ConnectionString

                Dim fileNameWithExtension As String = "example.png"
                Dim filePath As String = "/WhereYourImagesAre/"

                ' requires being able to use path
                Dim blobName As String = Path.GetFileNameWithoutExtension(fileNameWithExtension)

                Dim container As BlobContainerClient = New BlobContainerClient(AzureConnectionString, containerName)
                container.CreateIfNotExists()

                Dim blob As BlobClient = container.GetBlobClient(fileNameWithExtension)

                Dim blobHeader = New Models.BlobHttpHeaders
                blobHeader.ContentType = Me.getFileContentType(fileNameWithExtension) '("image/png")

                blob.UploadAsync(filePath & fileNameWithExtension, blobHeader)
                return "bob upload!!!"
            End Function

            Public Function getFileContentType(pFileWithExtension As String) As String
                Dim ContentType As String = String.Empty
                Dim Extension As String = Path.GetExtension(FilePath).ToLower()
                
                ' may want more extension types based on your needs
                Select Case Extension
                    Case ".gif"
                        ContentType = "image/gif"
                    Case ".jpg"
                        ContentType = "image/jpeg"
                    Case ".jpeg"
                        ContentType = "image/jpeg"
                    Case ".png"
                        ContentType = "image/png"
                End Select
                
                return ContentType;
            End Function
Related