Node.js 如何以多方形式接收json对象和文件?

Node.js 如何以多方形式接收json对象和文件?,node.js,multipartform-data,formidable,node.js-connect,Node.js,Multipartform Data,Formidable,Node.js Connect,我试图建立一个angularJS页面,发布用户名、密码和个人资料。我使用自定义fileUpload指令,然后将字段和文件作为多部分表单请求发送 在服务器端,我可以使用multiparty正确地获取文件,但是字段数据显示为{[object]},我无法访问它。尝试了JSON.stringify,但也不起作用 这是我的密码: 查看 <form ng-submit="submitForm(user)"> <input type="text" ng-model="

我试图建立一个angularJS页面,发布用户名、密码和个人资料。我使用自定义fileUpload指令,然后将字段和文件作为多部分表单请求发送

在服务器端,我可以使用multiparty正确地获取文件,但是字段数据显示为{[object]},我无法访问它。尝试了JSON.stringify,但也不起作用

这是我的密码:

查看

<form ng-submit="submitForm(user)">
            <input type="text" ng-model="user.username" placeholder="user name">
            <input type="text" ng-model="user.age" placeholder="age">
            <input type="password" ng-model="user.password"      placeholder="password">
            <input type="file" file-upload multiple/>
            <input type="submit" class="btn btn-danger">Send</button>
        </form>
理想情况下,我希望看到的是以JSON的形式接收用户{}对象,这样我就可以在mongo中创建一个记录,获取记录_id并使用它来命名我的文件。 注意,我可以很好地接收文件和字段。我只需要帮助将传入字段转换为JSON


非常感谢您提供的任何帮助。

问题已解决。将数据作为“字段”获取,然后使用json.parse()将其转换为json对象


请确保您的审查-将有助于您的调试工作…当然,必须声明字段[]数组。此处未显示
var app = angular.module('testphoto', []);

app.directive('fileUpload', function () {
    return {
        scope: true,        
        link: function (scope, el, attrs) {
            el.bind('change', function (event) {
                var files = event.target.files;

                for (var i = 0;i<files.length;i++) {
                    scope.$emit("fileSelected", { file: files[i] });
                }                                       
            });
        }
    };
});



app.controller('photoController', function($scope, $http){
    $scope.files = [];

    //listen for the file selected event
    $scope.$on("fileSelected", function (event, args) {
        $scope.$apply(function () {            
            //add the file object to the scope's files collection
            $scope.files.push(args.file);
            console.log('$scope.files has', $scope.files)
        });
    });


    $scope.submitForm = function(user) {

       $http({
                method: 'POST',
                url: 'http://localhost:3050/user',
                headers: { 'Content-Type': undefined },
                transformRequest: function(data) {
                var fd = new FormData();
                fd.append('user', user);
                for (var i = 0; i < data.files.length; i++) {
                fd.append('file' + i, data.files[i]);
                }
                return fd;
                },
                data: { model: $scope.model, files: $scope.files }
                }).
        success(function (data, status, headers, config) {

        }).
        error(function (data, status, headers, config) {
            alert("failed!");
        });
    };
});
app.post('/user', function (req, res) {
var count=0
var form = new multiparty.Form();
var uploadDir = __dirname + '/../uploads/fullsize/'
var size = '';
var fileName = '';
var data = {}
var fields = []
var fieldCount = 0

form.on('field', function(name, val){
  fields.push('{"'+name+'":'+val+'}')
  console.log('fields array now has:', fields[fieldCount])
  var fieldsStringified = JSON.stringify(fields[fieldCount])
  console.log('fieldsStringified:',fieldsStringified)
  fieldCount++
      });


form.on('file', function(name,file){
    count++
    var tmp_path = file.path
  var target_path = uploadDir + 'profilePic' + count+'.jpg';
    mv(tmp_path, target_path, function(err){
      if (err) {
          console.error(err.stack)
      }
        console.log('file '+target_path+ 'saved' )


      })


    }) //--- end app.post()
//Push field onto an array
form.on('field', function(name, val){
          fields.push('"' +name+ '"'+ ':'+'"'+val+'"')
         });
//finally convert array to json obj
form.on('close', function(){
  var str = '{'+fields.toString() +'}'
  var user = JSON.parse(str)