<input type= "File" ng-file-select= "Onfileselect ($files)" accept= "image/*" >
App.factory ("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
};
});
How to do this after you select a picture
$scope. onfileselect = function (files) {
var filein=files[0];
var img = new Image ();
var fileType = fileIn.name.substring (FileIn.name.lastIndexOf (".") + 1, fileIn.name.length);
if (filein.size>5242880) {//unit is B, no more than 5M is allowed here
Alert ("Picture cannot exceed 5M")
Return
}
if (filetype== ' jpg ' | | | filetype== ' PNG ' | | | | filetype== ' JPEG ' | | filetype== ' jpg ' | | filetype== ' PNG ' | | | filetype== ' JPEG ') {
}else{
Alert ("Picture format supports: Jpg,png,jpeg only")
Return
}
Filereader.readasdataurl (Filein, $scope)
. then (function (Result) {
$scope. imagesrc = result;
Console.log (Img.width);
});
}