【问题标题】:Multer not accepting files in array format gives 'Unexpected File Error'Multer 不接受数组格式的文件会给出“意外文件错误”
【发布时间】:2015-12-31 06:44:15
【问题描述】:

Multer 是一个与 node js 和 express 一起使用的模块,用于上传文件。我在角度端使用 ng-file 上传模块。

当我一个接一个地发送多个文件时,它工作得很好,没有任何错误,但是当我以数组格式一次性发送所有文件,然后我按照 Multer 的 github 的建议在服务器端进行必要的更改时,仍然出现错误。

这是错误

Error: Unexpected field
    at makeError (C:\nodefiles\new\node_modules\multer\lib\make-error.js:12:13)
    at wrappedFileFilter (C:\nodefiles\new\node_modules\multer\index.js:39:19)
    at Busboy.<anonymous> (C:\nodefiles\new\node_modules\multer\lib\make-middleware.js:109:7)
    at Busboy.emit (events.js:118:17)
    at Busboy.emit (C:\nodefiles\new\node_modules\multer\node_modules\busboy\lib\main.js:31:35)
    at PartStream.<anonymous> (C:\nodefiles\new\node_modules\multer\node_modules\busboy\lib\types\multipart.js:209:13)
    at PartStream.emit (events.js:107:17)
    at HeaderParser.<anonymous> (C:\nodefiles\new\node_modules\multer\node_modules\busboy\node_modules\dicer\lib\Dicer.js:51:16)
    at HeaderParser.emit (events.js:107:17)
    at HeaderParser._finish (C:\nodefiles\new\node_modules\multer\node_modules\busboy\node_modules\dicer\lib\HeaderParser.js:70:8)
    at SBMH.<anonymous> (C:\nodefiles\new\node_modules\multer\node_modules\busboy\node_modules\dicer\lib\HeaderParser.js:42:12)
    at SBMH.emit (events.js:118:17)
    at SBMH._sbmh_feed (C:\nodefiles\new\node_modules\multer\node_modules\busboy\node_modules\dicer\node_modules\streamsearch\lib\sbmh.js:159:14)
    at SBMH.push (C:\nodefiles\new\node_modules\multer\node_modules\busboy\node_modules\dicer\node_modules\streamsearch\lib\sbmh.js:56:14)
    at HeaderParser.push (C:\nodefiles\new\node_modules\multer\node_modules\busboy\node_modules\dicer\lib\HeaderParser.js:48:19)
    at Dicer._oninfo (C:\nodefiles\new\node_modules\multer\node_modules\busboy\node_modules\dicer\lib\Dicer.js:198:25)

示例控制器代码

var app = angular.module('fileUpload', ['ngFileUpload']);

app.controller('MyCtrl', ['$scope', 'Upload', '$timeout', function ($scope, Upload, $timeout) {
    $scope.uploadFiles = function (files) {
        $scope.files = files;
        if (files && files.length) {
            console.log(files);
            Upload.upload({
                url: '/api/data/addtweet',
                data: {
                    files: files
                }
            }).then(function (response) {
                $timeout(function () {
                    $scope.result = response.data;
                });
            }, function (response) {
                if (response.status > 0) {
                    $scope.errorMsg = response.status + ': ' + response.data;
                }
            }, function (evt) {
                $scope.progress =
                    Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
            });
        }
    };
}]);

请告诉我我做错了什么。 谷歌搜索并没有那么有用,我已经尝试过了,这就是我在这里发帖的原因。

【问题讨论】:

    标签: javascript angularjs node.js express multer


    【解决方案1】:

    错误原因是multer目前不支持ng-file-upload默认使用的数组语法files[0]files[1]files[2]multer期待一个系列具有相同字段名称的文件

    最简单的解决方案是像这样设置ng-file-uploadarrayKey 选项以避免附加[index] 部分:

    Upload.upload({
      url: '/api/data/addtweet',
      arrayKey: '', // default is '[i]'
      data: {
        files: files
      }
    })
    

    【讨论】:

    • 非常感谢@mscdex。
    • 我也遇到了同样的问题。通过上面的操作。像 arrayKey:'', 'arguments to path.join must be strings' 会发生错误,但是通过使用 arrayKey:' '(空格 b/w 引号),会显示相同的错误(即意外的字段错误)。你能帮帮我吗
    【解决方案2】:

    如果有人在上传自定义表单数据对象时遇到类似问题,您可以使用此方法。在这里我没有使用 ngFileUpload。

    客户端代码

    var fd = new FormData();
    
    for( var i =0; i< files.length ; i++ ){
        fd.append('fileItems' , files[i] );
    }
    
    fd.append('_id', params._id );              
    fd.append('user', params.user );              
    
    return $http.post( ROOT_URL + '/uploadFiles/', fd, {
        transformRequest: angular.identity,
        headers: {'Content-Type': undefined }
    });
    

    快速路由器

    var multer  = require("multer");
    var upload = multer({ dest: "uploads/" });
    
    app.post('/api/uploadFiles', upload.array('fileItems'), handlers.files.uploadFiles);
    

    【讨论】:

    • 不会导致这个问题,因为问题的根本原因是 ng-fileupload 通过以数组格式列出多个文件来发送多个文件,而您正在做同样的事情。
    • 不。这解决了这个问题,因为我附加了具有相同密钥的文件。 fd.append('fileItems' , 文件[i] ); .我在我的项目中使用此代码没有任何问题。问题是当你尝试做类似 fd.append('fileItems' , files );其中 files 是一个数组。
    • 您确实在添加具有不同字段名称的文件,对吧?但无论如何,如果这有效,那么我无法添加任何内容,只要我有空闲时间,我就会检查这个解决方案。干杯!
    猜你喜欢
    • 2021-08-15
    • 2017-05-17
    • 1970-01-01
    • 2013-09-10
    • 2023-01-16
    • 2022-01-23
    • 2017-10-22
    • 1970-01-01
    相关资源
    最近更新 更多