I was trying to convert a file to byte array using angularjs. its working fine, also add the byte code and filename to an array($scope.FileAttachments), and when it'll be added to $scope.FileAttachments client side ng-repet will display the attached file. file conversion done, file added to $scope.FileAttachments also done, but ng-repeat not working at right time. interesting issue is that, when any other event happened inside the view, ng-repeat working nicely.
HTML Code
<input id="File1" ng-model="File1" onchange="angular.element(this).scope().file1Upload()" type="file" />
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody id="tblAttachments">
<tr ng-repeat="item in FileAttachments track by $index">
<td>{{item.AttachmentDescription}}</td>
</tr>
</tbody>
</table>
AngularJS Controller's Code
$scope.FileAttachments = [];
var fileData;
function getBuffer(resolve) {
var reader = new FileReader();
reader.readAsArrayBuffer(fileData);
reader.onload = function () {
var arrayBuffer = reader.result
var bytes = new Uint8Array(arrayBuffer);
resolve(bytes);
}
}
$scope.file1Upload=function() {
var files = document.getElementById("File1").files;
fileData = new Blob([files[0]]);
var promise = new Promise(getBuffer);
promise.then(function (data) {
$scope.FileAttachments.push({
"AttachmentDescription": files[0].name.toString(),
"FileValue": data.toString()
});
}).catch(function (err) {
console.log('Error: ', err);
});
}
$scope.FileAttachmentsoutside the scope of your controller, you'll have to call$scope.$apply()to update the scope.