Azure Blob always downloads when navigating to url Azure Blob always downloads when navigating to url google-chrome google-chrome

Azure Blob always downloads when navigating to url


This is because you didn't set the content type property of the blob (the default is application/octet-stream, which triggers a download in most browsers). If you want PDF files to show correctly you'll need to change the content type of your PDF files to application/pdf (image/jpeg for jpeg files).

You can change the content type with common tools like Azure Storage Explorer, Cloud Storage Studio, CloudBerry, CloudXplorer, ... or by using the SDK. Note that some of these tools will automatically set the content type to the right one after uploading the file.


   blob.Properties.ContentType = "application/pdf";

//Get the content type of the file by extension

    public static string GetFileContentType(string FilePath)    {        string ContentType = String.Empty;        string Extension = Path.GetExtension(FilePath).ToLower();        switch (Extension)        {            case ConstantUtility.FILE_EXTENSION_PDF:                ContentType = "application/pdf";                break;            case ConstantUtility.FILE_EXTENSION_TXT:                ContentType = "text/plain";                break;            case ConstantUtility.FILE_EXTENSION_BMP:                ContentType = "image/bmp";                break;            case ConstantUtility.FILE_EXTENSION_GIF:                ContentType = "image/gif";                break;            case ConstantUtility.FILE_EXTENSION_PNG:                ContentType = "image/png";                break;            case ConstantUtility.FILE_EXTENSION_JPG:                ContentType = "image/jpeg";                break;            case ConstantUtility.FILE_EXTENSION_JPEG:                ContentType = "image/jpeg";                break;            case ConstantUtility.FILE_EXTENSION_XLS:                ContentType = "application/vnd.ms-excel";                break;            case ConstantUtility.FILE_EXTENSION_XLSX:                ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";                break;            case ConstantUtility.FILE_EXTENSION_CSV:                ContentType = "text/csv";                break;            case ConstantUtility.FILE_EXTENSION_HTML:                ContentType = "text/html";                break;            case ConstantUtility.FILE_EXTENSION_XML:                ContentType = "text/xml";                break;            case ConstantUtility.FILE_EXTENSION_ZIP:                ContentType = "application/zip";                break;            default:                ContentType = "application/octet-stream";                break;        }        return ContentType;    }

Use this to set the content type of the blob while saving it.

Set Content-type of media files stored on Blob


For those uploading files via PowerShell, use the below syntax to set content type during the upload.

Set-AzureStorageBlobContent -File <localFilePath> -Container <containerName> -Properties @{"ContentType"="text/plain"} -Context $ctx

Above I set the blob content type to text/plain, useful when uploading JSON and HTML files that will be used with templates. List of more content types header values here.