Pages

Wednesday, May 10, 2017

Convert uploaded file to array buffer


I would like to share some code snippet which will convert uploaded file to array buffer which we used mostly when upload something to SharePoint using JavaScript. Below is the code snippet for conversion,

var imageAsArrayBuffer;

//Get files from upload control 
var files = document.getElementById("picturefile").files;

fileData = new Blob([files[0]]);
var promise = new Promise(getBuffer);
promise.then(function(data) {       
     imageAsArrayBuffer = data;
}).catch(function(err) {
       alert('Error: ',err);
});

function getBuffer(resolve) {
var reader = new FileReader();
reader.readAsArrayBuffer(fileData);
reader.onload = function() {
 var arrayBuffer = reader.result
 //var bytes = new Uint8Array(arrayBuffer);
 resolve(arrayBuffer);
}

}

1 comment:

  1. Very interesting program - Convert uploaded file to array buffer

    Well, check out our digital marketing agency

    SEO services

    Waiting for your future posts...!

    ReplyDelete