JavaScript Angular + image upload. Having trouble getting form data to back end angular image upload with preview,angular image upload component,angular

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.

Answer:1

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) {...

  1. javascript hoisting functions
  2. javascript hoisting why
  3. javascript hoisting let
  4. javascript hoisting quiz
  5. javascript hoisting const
  6. javascript hoisting es6
  7. javascript hoisting stack overflow
  8. javascript hoisting questions
  9. javascript hoisting interview questions
  10. javascript hoisting medium
  11. javascript hoisting mdn
  12. javascript hoisting tutorial
  13. javascript hoisting variables
  14. javascript hoisting definition
  15. javascript hoisting meaning
  16. javascript hoisting not working
  17. javascript hoisting advantages
  18. javascript hoisting order
  19. javascript hoisting problem
  20. javascript hoisting inside function

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 ...

  1. styling input validation
  2. custom field validation message

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) ....

  1. callback hell with promises
  2. callback hell in javascript
  3. callback hell in node js
  4. callback hell in js
  5. callback hell in node js example
  6. node js callback hell
  7. callback hell in node
  8. callback hell in java
  9. callback hell in angular 2
  10. callback hell in angular
  11. callback hell in swift
  12. callback hell in angularjs
  13. callback hell in react
  14. callback hell in javascript stackoverflow
  15. callback hell in angular 6
  16. callback hell in es6
  17. callback hell to rxjs
  18. callback hell in
  19. callback hell in typescript
  20. callback hell in ios

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 ...