How to convert mutilple files to List of byte array using angularjs ? [Resolved]

Posted by Kasani007 under AngularJS on 4/3/2017 | Points: 10 | Views : 530 | Status : [Member] | Replies : 3
How to convert mutilple files to List of byte array using angularjs ?




Responses

Posted by: Kasani007 on: 4/3/2017 [Member] Starter | Points: 25

Up
0
Down

Resolved
http://stackoverflow.com/questions/9815625/looping-through-files-for-filereader-output-always-contains-last-value-from-loo


for (var i = 0; i < files.length; i++) { //for multiple files
(function(file) {
var name = file.name;
var reader = new FileReader();
reader.onload = function(e) {
// get file content
var text = e.target.result;
var li = document.createElement("li");
li.innerHTML = name + "____" + text;
ul.appendChild(li);
}
reader.readAsText(file, "UTF-8");
})(files[i]);
}
Alternately, you can define a named function and call it as normal:

function setupReader(file) {
var name = file.name;
var reader = new FileReader();
reader.onload = function(e) {
// get file content
var text = e.target.result;
var li = document.createElement("li");
li.innerHTML = name + "____" + text;
ul.appendChild(li);
}
reader.readAsText(file, "UTF-8");
}

for (var i = 0; i < files.length; i++) {
setupReader(files[i]);
}

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 4/3/2017 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Try this

$scope.onFileSelect = function($file){
var reader = new FileReader();
reader.onload = function(e){
$scope.encoded_file = btoa(e.target.result.toString());
};
reader.readAsBinaryString($file[0]);
};


Source : http://stackoverflow.com/questions/32384702/how-to-convert-html-file-to-byte-array-using-angularjs

--
Thanks & Regards,
RNA Team

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kasani007 on: 4/3/2017 [Member] Starter | Points: 25

Up
0
Down
Hi mr. Raj ,

what is "btoa" ?

and my code should be as:

$scope.onFileSelect = function($file){ // where $file is array
var reader = new FileReader();
reader.onload = function(e){
$scope.encoded_file = btoa(e.target.result.toString());
};
for(var i=0;i<$file.length;i++){
reader.readAsBinaryString($file[i]);
}
};


but it is not working

Kasani007, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response