簡體   English   中英

如何更改“ ng2-file-upload”默認行為,以將輸入的文件的名稱屬性更改為“ file”

[英]How can i change 'ng2-file-upload' default behavior to change the name attribute of the file input to 'file'

我正在嘗試構建一個有角度的表單,在該表單中我將上傳2張不同的照片。

我在nodejs上使用`` multer '',在angular中使用`` ng2-file-upload ''。

我需要為每張照片使用唯一的標識符,以便能夠正確處理它們。

multer設置為使用html name屬性。 問題是ng2-file-upload在默認情況下會將輸入類型file的屬性名稱更改為字符串文件

當我嘗試運行此代碼時:

Node.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-file-upload的默認行為,以將輸入的文件的name屬性更改為“ file”嗎?

非常感謝!

ps

告訴multer接受any()對我沒有幫助。 我需要能夠識別這2個不同的輸入。

只是遇到了同樣的問題,有一個簡單的解決方法可以解決此問題。

在上傳文件之前,更改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()
}

解決方案是從這里采取的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM