2

我正在尝试构建一个有角度的表单,我将在其中上传 2 张不同的照片。

我在 nodejs 上使用“ multer ”,在 angular中使用“ ng2-file-upload ”。

我需要为每张照片设置一个唯一标识符,以便能够正确处理它们。

multer设置为使用 html name 属性。问题是ng2-file-upload默认将输入类型 file -的属性名称更改为字符串file

当我尝试运行此代码时:

节点.js:

const upload = multer({
    storage
    }).fields([
       { name: 'posterPic'},
       { name: 'widePic'}
  ]);

角度:

<div class="form-group">
    <input type="file" id="posterPic" [name]="posterPic" ng2FileSelect [uploader]="uploader">
</div>
 <div class="form-group">
    <input type="file" id="widePic" [name]="widePic" ng2FileSelect [uploader]="uploader">
 </div>

上传时 - 'ng2-file-upload' 将name="posterPic"and更改name="widePic"name="file".

错误:

{ [Error: Unexpected field]
  code: 'LIMIT_UNEXPECTED_FILE',
  field: 'file',
  storageErrors: [] }

任何人都知道如何更改ng2-file-upload默认行为以将文件输入的名称属性更改为“文件”?

非常感谢!

ps

告诉 multer 接受 any() 对我没有帮助。我需要能够识别这 2 个不同的输入。

4

1 回答 1

1

刚遇到同样的问题,有一个简单的解决方法可以解决这个问题。

就在您上传文件之前,更改 FileItem 别名属性,这将更改表单数据名称属性值。

因此,例如在您看来:

<input type="file" (onFileDrop)="startUpload()" ng2FileSelect [uploader]="uploader">

并在您的组件中:

startUpload(){
    this.uploader.queue[0].alias = "posterPic"  // Or your file name
    this.uploader.queue[0].upload()
}

解决方案取自这里

于 2019-04-29T08:16:22.223 回答