An Angular wrapper around the FileReader API
- bower:
bower install lr-file-reader - add the script lrFileReader.js to your application
- register the module to your app
angular.module('myApp,['lrFileReader'])
At tropicaldev we use it to generate a preview of the image the user intends to upload thanks to the method readAsDataURL
you create a file reader passing a Blob or a file then any of the method of the FileReader API is exposed and will return a promise which will resolve with the result or be rejected with the error (or in case you call abort() ). You will also be able to register to any of the event of the API.
function controller($scope,lrFileReader){
//assuming $scope.file is a blob or file
lrFileReader($scope.file)
.on('progress',function(event){
$scope.progress=event.loaded/event.total;
})
.on('load',function(event){
console.log('finished')
})
.readAsDataURL()
.then(function(result){
$scope.previewSrc=result;
});
}see the example