Here is a task I thought might be easy, but I really don't get it. There is a dropzone on an empty page. The user can drop files and the information of the file is then going to be stored in a database.
But the function that passes the information, only passes the last file dropped. But if I log the base64URL in the filereader.onload event all URLs are logged to the console.
dropzone.addEventListener('drop', function(e) {
e.preventDefault();
e.stopPropagation();
dropzone.classList.remove('highlight');
files = e.dataTransfer.files;
if(!files) return;
for (var i = 0; i < files.length; i++) {
var file = files[i];
var getFileName = file.name;
var getFileSize = file.size;
var getFileModifiedDate = file.lastModifiedDate;
var base64single;
console.log(i + " " + getFileName + " " + getFileSize + " " + getFileModifiedDate);
var fileReader = new FileReader();
fileReader.onload = function (e) {
base64single = e.target.result;
console.log(base64single);
Microsoft.Dynamics.NAV.InvokeExtensibilityMethod('sendFileData', [base64single, getFileName, getFileSize, getFileModifiedDate]); //This function passes to a database
};
fileReader.readAsDataURL(file);
}
}, false);
How do I have to change this to make it work? I would try with promises but I dont understand how they work yet.