我正在製作一個簡單的文件上傳腳本,並且只能找到基於input
的(change)
事件的工作示例。即 - https://www.thepolyglotdeveloper.com/2016/02/upload-files-to-node-js-using-angular-2/Angular 2 - 文件上傳 - 如何訪問文件?
<input type="file" id="userfile" class="form-control"
(change)="fileChangeEvent($event)" name='userfile'>
This works。這很好。
但是,如果我嘗試綁定文件輸入到ngModel它不起作用。
模板
<form class="form-signin" (ngSubmit)="onSubmit(fileForm.value)" #fileForm="ngForm">
<input type="file" id="userfile" class="form-control"
[(ngModel)]="fileUpload.userfile" name='userfile'>
<input type="text" id="random" class="form-control"
[(ngModel)]="fileUpload.random" name="random">
<button class="btn btn-lg btn-primary btn-block" type="submit">Upload File</button>
</form>
component.ts
onSubmit(data){
console.log("Submitted");
console.log(data);
}
只有數據在random
輸入設置會顯示出來。沒有文件放置在文件輸入將顯示出來,只有(在我的ngModel和打印出來的數據)(undefined)onSubmit(data)
[Angular 2 File input from input type = file]可能重複(http://stackoverflow.com/questions/35399617/angular-2-file-upload-from-input-type-file) – Adam