Question:
I want to send an image (I'll save it in a database), but I don't know how to send it together in POST
.
I'm using AngularJs
:
<input type="file" ng-model="user.imageProfile">
$scope.register = function (user) {
if (user.password != user.passwordConfirm) {
$rootScope.showToast("Confirmação de senha inválida!");
return null;
}
$http.post($rootScope.serviceBase + "users", user).then(function () {
$rootScope.showToast("Cadastrado com sucesso");
$mdDialog.cancel();
});
};
Answer:
I decided to convert the image to Base64
, I used angular-base64-upload , it looked like this:
Input:
<input type="file" ng-model="image" base-sixty-four-input>
Controller:
$scope.register = function (user) {
if (user.password != user.passwordConfirm) {
$rootScope.showToast("Confirmação de senha inválida!");
return null;
}
$scope.user.imageProfile = $scope.image.base64;
$http.post($rootScope.serviceBase + "users", user).then(function () {
$rootScope.showToast("Cadastrado com sucesso");
$mdDialog.cancel();
});
};
Exhibition:
<img data-ng-src="data:image/png;base64,{{ userAuthenticated.imageProfile }}" title="{{ userAuthenticated.name }}" class="photoUser" data-err-src="images/png/avatar.png"/>