【问题标题】:FormidableJS form does not parse when posted from AngularJS从 AngularJS 发布时,FormidableJS 表单无法解析
【发布时间】:2014-03-13 15:44:49
【问题描述】:

我从 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); ` 
...

当使用 formData 对象发布表单 Angular 时,我必须将内容/类型设置为未定义,如评论 here 中所述。这可能是强大的问题吗?

我一直在用几个小时试图解决这个问题,但没有运气。如果有任何答案,我将不胜感激!

【问题讨论】:

    标签: node.js angularjs express formidable


    【解决方案1】:

    我想通了。我必须将文件绑定到控制器,以及使用 angular.identity 函数。基于this great explanation,我在我的角度代码中添加了以下内容,以便将文件绑定到控制器:`

    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]);
                    });
                });
            }
        };
    }]);
    

    然后将我的表单发布到我想要的网址:

    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 中,files-inputs 获取以下标签以使用创建的文件模型:

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

    我的文本输入只是像以前一样绑定:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-12
      • 2010-09-30
      • 2016-01-11
      • 1970-01-01
      • 2019-10-27
      • 1970-01-01
      • 2013-12-03
      • 2015-08-03
      相关资源
      最近更新 更多