0

我正在使用 Angular 和 ng2-file-upload。我收到一个错误:

{ Error: Unexpected field
at makeError (C:\Users\cDotsp27598345\Desktop\Projects\ZeoliteGlobal\node_modules\multer\lib\make-error.js:12:13)
at wrappedFileFilter (C:\Users\cDotsp27598345\Desktop\Projects\ZeoliteGlobal\node_modules\multer\index.js:40:19)
at Busboy.<anonymous> (C:\Users\cDotsp27598345\Desktop\Projects\ZeoliteGlobal\node_modules\multer\lib\make-middleware.js:114:7)
at Busboy.emit (events.js:159:13)
at Busboy.emit (C:\Users\cDotsp27598345\Desktop\Projects\ZeoliteGlobal\node_modules\busboy\lib\main.js:38:33)
at PartStream.<anonymous> (C:\Users\cDotsp27598345\Desktop\Projects\ZeoliteGlobal\node_modules\busboy\lib\types\multipart.js:213:13)
at PartStream.emit (events.js:159:13)
at HeaderParser.<anonymous> (C:\Users\cDotsp27598345\Desktop\Projects\ZeoliteGlobal\node_modules\dicer\lib\Dicer.js:51:16)
at HeaderParser.emit (events.js:159:13)
at HeaderParser._finish (C:\Users\cDotsp27598345\Desktop\Projects\ZeoliteGlobal\node_modules\dicer\lib\HeaderParser.js:68:8)
  code: 'LIMIT_UNEXPECTED_FILE',
  field: 'file',
  storageErrors: [] }

这是我的组件的 HTML:(该addEvent函数调用uploadPicture下面的函数。)

<form #f="ngForm"(ngSubmit)="addEvent(f.value)">
    <input type="file" name="pic" ng2FileSelect [uploader]="uploader"/>
    <button type="submit">Submit</button>
</form>

我的角组件:

  uploadPicture() {
    for (const item of this.uploader.queue) {
      if (!item.isUploaded) {
        item.upload();
      }
    }
  }

和上传者:

  public uploader: FileUploader = new FileUploader({url: this.imgUpldURL, authToken: `Bearer ${localStorage.getItem('token')}`});

还有我的 API 端点(我使用的是动态 ID,如果我提供没有动态 ID 的路径,它仍然不起作用):

const multer = require('multer');

router.post('/uploadEventImage', authCheck, (req, res) => {
    const id = req.body.id;
    const upload = multer({dest: 
    `../src/assets/images/news/${id}/`}).single('pic');
    console.log('called')
    upload(req, res, function (err) {
        if (err) {
            console.log(err);
          return res.end(err.toString());
        }
        console.log('done');
        res.end('File is uploaded');
      });    
});

这是我后端的所有 multer 配置。请求被成功接收。我在网上发现的大多数问题都是由于输入的名称属性与传入的不同.single('name'),但是正如您所看到的,这并没有解决任何问题。

4

1 回答 1

2

原来ng2-file-upload是将所有字段的名称设置为file.

于 2018-02-08T09:21:05.920 回答