I have been struggling to include a profile picture upload along with regular text data and send that all to the back end to create a new user through mongoose. I have tried everything from ng-file-upload/angular-file-upload to building a custom directive but to not avail I still can't get the image or the data to make it to the server side.
My photo upload html snippet can be seen below:
<div ng-show="!Login">
<label class="col-xs-12 col-sm-4">Profile Picture</label>
<div class="col-xs-12 col-sm-4">
<input type="file" file-input="files" multiple/>
<button ng-click="upload()">Upload</button>
</div>
</div>
I've created a custom directive "file-input" that looks like this:
app.directive('fileInput', ['$parse', function($parse){
return {
restrict: "A",
link: function(scope, elm, attrs){
elm.bind("change", function(){
$parse(attrs.fileInput)
.assign(scope, elm[0].files)
scope.$apply()
});
}
}
}])
and my controller looks like this:
$scope.upload = function(){
var fd = new FormData();
fd.append('file', $scope.files[0])
console.log(fd);
userFactory.addPhoto(fd)
.success(function(data){
console.log("Data: ", data);
})
}
this is my service for the form data post to the backend:
userFactory.addPhoto = function(fd){
return $http.post('/users/addPhoto', fd, {
transformRequest:angular.identity,
headers:{
'Content-Type': undefined
}
})
}
So the problem with this is that when I send the data to the backend and it gets there I get a blank req.body object with nothing inside of it
router.post('/addPhoto', function(req, res, next){
console.log(req.body);
// var profilePicture = new Buffer(req.file.buffer, 'base64').toString('ascii')
// console.log(profilePicture);
});
What's happening here? I'd like to get the image in a buffer in the backend so i can base64 encode it and store it in mongodb with mongoose.
If anyone has a way to send both the image and text data together by passing them into a ng-click function, getting them to the controller so I can put them both in an object, and then sending that object through to the backend together to register a new user I would greatly prefer that solution.
In my code, the x value is undefined. If I remove if block, the x value is displayed as 77. I don't understand why if block is modifying the x value. var x = 77; function fn() { if (false) {...
In my code, the x value is undefined. If I remove if block, the x value is displayed as 77. I don't understand why if block is modifying the x value. var x = 77; function fn() { if (false) {...
I have a JavaScript validation on form, every time the form is submitted it will empty the boxes. I need to change it, when the form is filled up incomplete with empty boxes. it will not clear all the ...
I have a JavaScript validation on form, every time the form is submitted it will empty the boxes. I need to change it, when the form is filled up incomplete with empty boxes. it will not clear all the ...
I got myself stuck in a pyramid of doom using Promises. I have the following: getA getB getC getC depends on getB (and getA) that depends on getA. So I must call them like this getA(param) ....
I got myself stuck in a pyramid of doom using Promises. I have the following: getA getB getC getC depends on getB (and getA) that depends on getA. So I must call them like this getA(param) ....
I am trying to create a set of elements from CMS. I have reproduced the problem here with a set of rectangles that are being generated in Javascript. How can I dynamically add a "onmouseover" method ...
I am trying to create a set of elements from CMS. I have reproduced the problem here with a set of rectangles that are being generated in Javascript. How can I dynamically add a "onmouseover" method ...