我正在尝试以角度构建一个表单,我将在其中上传 2 张不同的照片。
我在nodejs上使用"multer",在angular中使用"ng2-file-upload"。
我需要每张照片的唯一标识符,以便能够正确处理它们。
Multer设置为使用 HTML 名称属性。 问题是ng2-file-upload默认将输入类型文件的属性名称更改为字符串文件。
当我尝试运行此代码时:
节点.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"
和name="widePic"
更改为name="file"
.
错误:
{ [Error: Unexpected field]
code: 'LIMIT_UNEXPECTED_FILE',
field: 'file',
storageErrors: [] }
有人知道如何更改ng2文件上传默认行为以将文件输入的name属性更改为"file"吗?
非常感谢!
附言
告诉 multer 接受 any(( 对我没有帮助。 我需要能够 识别这 2 个不同的输入。
刚刚遇到了同样的问题,有一个简单的解决方法可以解决这个问题。
在上传文件之前,更改文件项别名属性,这将更改表单数据名称属性值。
因此,例如在您看来:
<input type="file" (onFileDrop)="startUpload()" ng2FileSelect [uploader]="uploader">
在您的组件中:
startUpload(){
this.uploader.queue[0].alias = "posterPic" // Or your file name
this.uploader.queue[0].upload()
}
解决方案是从这里开始的。