我第一次從事一個從產品中收集資料的角度專案。雖然一切正常檔案,但我在影像上傳的 html 檔案中遇到錯誤。我收到此錯誤 “EventTarget”型別上不存在屬性“檔案”。
這是html檔案
<div class="row mb-3">
<label for="Product Image" class="col-sm-2 col-form-label">Product Image:</label>
<div class="form-group">
<input type="file" id="photo" name="photo" (change)="handleFileInput($event.target.files)">
</div>
</div>
這是我的 .ts 檔案
export class AddProductComponent implements OnInit {
model:any={}
fileToUpload: File = null;
handleFileInput(files:FileList){
this.fileToUpload=files.item(0);
console.log(this.fileToUpload);
const formData: FormData = new FormData();
formData.append('Image', this.fileToUpload);
this.http.post('http://localhost:3000/upload',formData).subscribe((res)=>{
console.log(res);
});
}
有人能幫我嗎...
uj5u.com熱心網友回復:
不要傳遞filesas 引數,$event而是傳遞整個引數:
<div class="row mb-3">
<label for="Product Image" class="col-sm-2 col-form-label">Product Image:</label>
<div class="form-group">
<input type="file" id="photo" name="photo" (change)="handleFileInput($event)">
</div>
</div>
handleFileInput(event){
this.fileToUpload=event.target.files[0];
console.log(this.fileToUpload);
const formData: FormData = new FormData();
formData.append('Image', this.fileToUpload);
this.http.post('http://localhost:3000/upload',formData).subscribe((res)=>{
console.log(res);
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/516231.html
標籤:有角度的打字稿事件处理
上一篇:如果JSON密鑰已經存在,有沒有辦法更新它,如果沒有,則在Redis上使用NodeRedisOM創建一個密鑰
下一篇:如何使用命名元組作為可選引數?
