Multer数组形式不接受文件给出了“意外提起错误” [英] Multer not accepting files in array format gives 'Unexpected Filed Error'

查看:428
本文介绍了Multer数组形式不接受文件给出了“意外提起错误”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Multer与节点JS和前preSS上传文件一起使用的模块。我使用的角度侧NG文件上传模块。

Multer is a module used along with node js and express to upload files. I am using ng-file upload module on the angular side.

当我发送多个文件逐个它工作得很好不用任何错误,但是当我送的所有文件一气呵成数组形式,然后通过Multer的github上的建议我提出在服务器端进行必要的更改,还是错误出现。

When I am sending multiple files one by one it works just fine without any errors whatsoever but when I am sending all files in one go in array format and then I am making necessary changes on the server side as suggested by Multer's github, still error comes.

下面是误差

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)

样控制器code

Sample Controller Code

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

请告诉我,我做错了。
谷歌搜索是没有多大用处,我已经尝试了i.e.why我张贴在这里。

Please tell me what I'm doing wrong. Google searches were not that useful, I have already tried that i.e.why I am posting here.

推荐答案

的原因错误是 multer 目前不支持<$ C $数组语法C> NG-文件上传默认使用的是文件[0] 文件[1] 文件[2] multer 期待一系列文件的相同的字段名称

The reason for the error is that multer currently does not support the array syntax that ng-file-upload uses by default which is files[0], files[1], files[2], etc. multer is expecting a series of files with the same field name.

最简单的办法是设置 NG-文件上传 arrayKey 选项象这样以避免追加 [指数] 部分:

The easiest solution is to set ng-file-upload's arrayKey option like so to avoid appending the [index] part:

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

这篇关于Multer数组形式不接受文件给出了“意外提起错误”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆