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

Notifications

Announcements

No record found.

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
    237,801 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
    237,801 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
    237,801 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Martin Dráb Profile Picture

Martin Dráb 664 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 522 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 303 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans