web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

How to set content type of file uploaded to Azure blob container

(0) ShareShare
ReportReport
Posted on by 18

Hi

with inspiration from this: http://axtechnicalblogger.blogspot.com/2019/11/upload-files-to-azure-blob-from-d365-fo.html 

I can upload a stream to a azure blob container, but the 'file' in the container is of type 'application/octet-stream' when in fact it is a 'application/pdf'

Here is some more information:

I'm reading emails from a Office 365 mailbox, and if there is any attachments and the attachment is of type PDF ('application/pdf') I store the file in a Azure blob container for futher processing.

Storing is working, but the type is not correct

I have tries omething like this: https://stackoverflow.com/questions/10040403/set-content-type-of-media-files-stored-on-blob

But I am not 'allowed' to set the content type

Any hint on how to do this is D365/x++ is highly appreciated

Thanks

I have the same question (0)
  • Martin Dráb Profile Picture
    239,040 Most Valuable Professional on at

    Can you please elaborate why you can't set the content type?

  • Verified answer
    CU03040925-0 Profile Picture
    18 on at

    Here is the code:

    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Blob;
    
    class SavefileToAzureBlobStorage
    {
        public static str saveFile(System.IO.Stream  _streamToSave, str _saveName, str _azureStorageAccountName, str _azureStorageAccountKey, str _azureBlobContainerName)
        {
            str ret;
            Microsoft.WindowsAzure.Storage.Auth.StorageCredentials storageCredentials;
            CloudStorageAccount storageAccount;
            
            try
            {
                storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(_azureStorageAccountName, _azureStorageAccountKey);
                if(storageCredentials)
                {
                    storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials, true);
                    if(storageAccount)
                    {
                        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                        CloudBlobContainer blobContainer = blobClient.GetContainerReference(_azureBlobContainerName);
                        blobContainer.CreateIfNotExistsAsync();
    
                        CloudBlockBlob blockblob =    blobContainer.GetBlockBlobReference(_savename);
                        if(blockblob && !blockblob.Exists(null, null))
                        {
                            blockblob.UploadFromStreamAsync(_streamToSave).Wait();
                            blockblob.FetchAttributes(null,null,null);
                            //blockblob.Properties.ContentType = "application/pdf";
                            BlobProperties BlobProperties = blockblob.Properties;
                            if(BlobProperties.Length == _streamToSave.Length)
                            {
                                ret = blockblob.Uri.AbsoluteUri;
                            }
                            
                        }
                        else
                        {
                            throw error(strFmt("File with name %1 already exists", _saveName));
                        }
                    }
                    else
                    {
                        throw error("Failed to get storeage account");
                    }
                }
                else
                {
                    throw errorr("Faild to create storage credentials");
                }
            }
            catch(Exception::Error)
            {
                throw Error(AifUtil::getClrErrorMessage());
            }
    
            return ret;
        }
    
    }

    If I uncomment the line where I set the content type I get a compilation error: Invalid token '=' 

  • Suggested answer
    Martin Dráb Profile Picture
    239,040 Most Valuable Professional on at

    Split it to two steps:

    BlobProperties blobProperties = blockblob.Properties;
    blobProperties.ContentType = "application/pdf";

    It seems that the compiler gets confused if you do it at once.

  • CU03040925-0 Profile Picture
    18 on at

    yes... But the property is not set :)

    I must be missing something else

  • Martin Dráb Profile Picture
    239,040 Most Valuable Professional on at

    I think it is set, but it's not used for anything. You should set it before uploading the data, shouldn't you?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 663

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 439 Super User 2026 Season 1

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 337 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans