var UploadController = function ($scope, fileReader) {
console.log(fileReader)
$scope.getFile = function () {
$scope.progress = 0;
fileReader.readAsDataUrl($scope.file, $scope)
.then(function(result) {
$scope.imageSrc = result;
});
};
$scope.$on("fileProgress", function(e, progress) {
$scope.progress = progress.loaded / progress.total;
});
};
app.directive("ngFileSelect",function(){
return {
link: function($scope,el){
el.bind("change", function(e){
$scope.file = (e.srcElement || e.target).files[0];
$scope.getFile();
})
}
}
})
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.0.x" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js" data-semver="1.0.8"></script>
<script> var app = angular.module('plunker', []); </script>
<script src="upload.js"></script>
<script src="app.js"></script>
</head>
<div ng-controller="UploadController ">
<form>
<input type="file" ng-file-select="onFileSelect($files)" >
<!-- <input type="file" ng-file-select="onFileSelect($files)" multiple> -->
</form>
<b>Preview:</b><br />
<i ng-hide="imageSrc">No image choosed</i>
<img ng-src="{{imageSrc}}" /><br/>
<b>Progress:</b>
<progress value="{{progress}}"></progress>
</div>
</html>
/* Put your css in here */
(function (module) {
var fileReader = function ($q, $log) {
var onLoad = function(reader, deferred, scope) {
return function () {
scope.$apply(function () {
deferred.resolve(reader.result);
});
};
};
var onError = function (reader, deferred, scope) {
return function () {
scope.$apply(function () {
deferred.reject(reader.result);
});
};
};
var onProgress = function(reader, scope) {
return function (event) {
scope.$broadcast("fileProgress",
{
total: event.total,
loaded: event.loaded
});
};
};
var getReader = function(deferred, scope) {
var reader = new FileReader();
reader.onload = onLoad(reader, deferred, scope);
reader.onerror = onError(reader, deferred, scope);
reader.onprogress = onProgress(reader, scope);
return reader;
};
var readAsDataURL = function (file, scope) {
var deferred = $q.defer();
var reader = getReader(deferred, scope);
reader.readAsDataURL(file);
return deferred.promise;
};
return {
readAsDataUrl: readAsDataURL
};
};
module.factory("fileReader",
["$q", "$log", fileReader]);
}(angular.module("plunker")));