Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 当从AngularJS发布时,FormidableJS表单不会解析_Node.js_Angularjs_Express_Formidable - Fatal编程技术网

Node.js 当从AngularJS发布时,FormidableJS表单不会解析

Node.js 当从AngularJS发布时,FormidableJS表单不会解析,node.js,angularjs,express,formidable,Node.js,Angularjs,Express,Formidable,我从AngularJS发布到一个强大的表单,但是表单没有解析。我不使用express.bodyParser(),我知道这经常是个问题 服务器端: ... var form = new formidable.IncomingForm(); console.log(form); //this prints //Get access to the other fields of the request. form.parse(req, function (err, fields, files) {

我从AngularJS发布到一个强大的表单,但是表单没有解析。我不使用express.bodyParser(),我知道这经常是个问题

服务器端:

...
var form = new formidable.IncomingForm();
console.log(form); //this prints
//Get access to the other fields of the request. 
form.parse(req, function (err, fields, files) {
    console.log("parsing.."); //this does not print
    console.log(fields);
    console.log(files);`
...
...
$http({
    method: 'POST',
    url: '/api/ad',
    data: message, // your original form data,
    transformRequest: formDataObject,  // this sends your data to the formDataObject provider that we are defining below. `[see this](https://stackoverflow.com/questions/17629126/how-to-upload-a-file-using-angularjs-like-the-traditional-way)`
    headers: {'Content-Type': undefined}
                    })
    .success(function(data, status, headers, config){
                        deferred.resolve(data); ` 
...
客户端:

...
var form = new formidable.IncomingForm();
console.log(form); //this prints
//Get access to the other fields of the request. 
form.parse(req, function (err, fields, files) {
    console.log("parsing.."); //this does not print
    console.log(fields);
    console.log(files);`
...
...
$http({
    method: 'POST',
    url: '/api/ad',
    data: message, // your original form data,
    transformRequest: formDataObject,  // this sends your data to the formDataObject provider that we are defining below. `[see this](https://stackoverflow.com/questions/17629126/how-to-upload-a-file-using-angularjs-like-the-traditional-way)`
    headers: {'Content-Type': undefined}
                    })
    .success(function(data, status, headers, config){
                        deferred.resolve(data); ` 
...
当使用formData对象发布表单时,我必须将内容/类型设置为未定义,如注释中所述。这可能是一个可怕的问题吗


我一直在用几个小时试图弄明白这一点,但运气不好。如果有任何答案,我将不胜感激

我发现了这一点。我必须将文件绑定到控制器,并使用angular.identity函数。基于,我在angular代码中添加了以下内容,以便将文件绑定到控制器:`

myApp.directive('fileModel', ['$parse', function ($parse) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            var model = $parse(attrs.fileModel);
            var modelSetter = model.assign;`

            element.bind('change', function(){
                scope.$apply(function(){
                    modelSetter(scope, element[0].files[0]);
                });
            });
        }
    };
}]);
下面是将我的表单发布到我想要的url的步骤:

myApp.service('fileUpload', ['$http', function ($http) {
    this.uploadFileToUrl = function(data, uploadUrl){
        var fd = new FormData();
        angular.forEach(data, function(value, key) {
            fd.append(key, value);
        });
        $http.post(uploadUrl, fd, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        })
        .success(function(retObj){
        console.log(retObj);
        })
        .error(function(retObj){
        console.log(retObj);
        });
    }
}]);
在控制器的提交功能中,我现在添加:

var formData = $scope.newForm;
        var uploadUrl = '/api/add';
        fileUpload.uploadFileToUrl(formData, uploadUrl);
在我的html中,文件输入获取以下标记以使用创建的文件模型:

<input type="file" file-model="newForm.image1">

我的文本输入和以前一样绑定:

<input type="text" ng-model="newForm.title">

希望这对其他人有帮助:)