How To upload a Local file into Microsoft Azure blob Storage?

How To upload a Local file into Microsoft Azure blob Storage?

To upload Local file into Azure blob storage from D365, following steps will be required:

  1. Firstly, create blob storage in azure.

Azure Storage Account

After that create a container in your Azure storage account. Once the container has been created, you need to create a CORS rule in it to allow the JavaScript running in the browser to access it. For testing purposes, I set everything to “*” . It will allow access for every dynamics instance, but this should be locked down when going live.

How to Edit CORS rule before uploading a Local file into Microsoft Azure
Edit CORS rule
  • 2. Then create an upload view on the form
Upload view
Upload view
  • 3. Then create a web Resource with Type Webtype(HTML) in D365 and Call a JS function on it and get file data in object
Web Resource
Web Resource
  •  4. However we need JavaScript Libraries to call azure services. You can use azure-storage.blob JS to use azure services and give reference to your HTML.
  •  5. Then Use the following code to upload into the blob. You will need SAS token and Storage name from azure.

var entityname = window.parent.Xrm.Page.data.entity.getEntityName();

var objectid = window.parent.Xrm.Page.data.entity.getId().substring(1, 37);

var owner = (window.parent.Xrm.Page.getAttribute(“ownerid”).getValue()[0].id).toString();

var data = {

“documentbody”: reader.result.split(‘,’)[1],

“filename”: ‘filename’,

“objectid”: ‘4fdsf44-ds4-34vdf’,//recordID

“username”: ‘crm account Username’,

“password”: ‘crm account password’

“url”: crmorganisationurl

};

// https:// storage.blob.core.windows.net

var blobUri = ‘https://’ + ‘storagename’ + ‘.blob.core.windows.net’;

//Get sas token from Azure Account

var SAS_TOKEN = ‘?sv=2017-11-09&ss=bfqt&srt=sco&sp=rwdlacup&se=2020-02-29T17:45:00Z&st=2018-10-11T09:45:00Z&spr=https,http&sig=t3uCoKT39AU%2F7CaI%2FnDICWExwF2KqC3sq9LAbSyfppg%3D’;

var blobService = AzureStorage.Blob.createBlobServiceWithSas(blobUri, SAS_TOKEN);

// If one file has been selected in the HTML file input element

var file = $(‘#FileInput’).get(0).files[0];

var customBlockSize = file.size > 1024 * 1024 * 32 ? 1024 * 1024 * 4 : 1024 * 512;

blobService.singleBlobPutThresholdInBytes = customBlockSize;

var finishedOrError = false;

var speedSummary = blobService.createBlockBlobFromBrowserFile(‘internal’, file.name, file, { blockSize: customBlockSize }, function (error, result, response) {

finishedOrError = true;

if (error) {

alert(‘Error’);

} else {

displayProcess(100);

}

});

So, with the help of the above steps, we can make business easy.

Tags: No tags

One Response

Add a Comment

Your email address will not be published. Required fields are marked *