I'm in this problem a days. What happens is the following when I try to click the update image button I can not do the image update on the first try despite my console.log it return me the correct value. What happens is that when I make the request to the backend the first time I load the update button it always returns the blob of the image that was previously and when I try again it returns the blob of the image that I tried to perform in the previous moment and Inserts into DB. In short he is always with a late request and I do not do the least because. I already tried to add the $ scope. $ Apply () and it did not work.
First update the blob of the previous image
Second update the blob correct of the image
In short I need to do update twice to be able to change the image to the one I want.
Code:
function uploadFile(){
var file = $scope.profilePic;
var blob = new Blob([file], {
"type": "text/html"
});
var reader = new FileReader();
reader.onload = function(e) {
$scope.text = e.target.result.split(',')[1];
$scope.loadedUser.profilePic = $scope.text;
}
reader.readAsDataURL(blob);
};
Html:
<div layout-gt-sm="row" style="margin-bottom: 20px;">
<input type="file" name="profilePic" fileread="profilePic">
</div>
APP:
app.directive("fileread", [function () {
return {
scope: {
fileread: "="
},
link: function (scope, element, attributes) {
element.bind("change", function (changeEvent) {
scope.$apply(function () {
scope.fileread = changeEvent.target.files[0];
// or all selected files:
// scope.fileread = changeEvent.target.files;
});
});
}
}
}]);