我必須隨圖像一起提交表單。我試過這個代碼(有多種方式),但沒有爲我工作。有沒有人有使用angular2的文件上傳工作演示,請幫助我。如何在Angular2中上傳文件
component.html
<form class="form-horizontal" role="form" >
<div class="form-group">
<label class="control-label col-sm-4" for="myname" style="">Name<span class="asterisk">*</span></label>
<div class="col-sm-7">
<div>
<input type="text" class="form-control" id="myname"
[(ngModel)]="myfile.name">
</div>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4" for="myimage">Image</label>
<div class="col-sm-7">
<div>
<input type="file" (change)="fileChangeEvent($event)" placeholder="Upload file..." />
</div>
</div>
</div>
<div class="form-group">
<div class="text-center">
<button type="button" (click)="upload()">Upload</button>
</div>
</div>
</form>
component.ts
myfile={
"name":"Mubashshir",
"image":''
}
fileChangeEvent(fileInput: any){
this.myfile.image = fileInput.target.files;
}
upload(){
this.base_path_service.PostRequest('http://128.199.190.109/api/school/schoolDetail/',this.myfile)
.subscribe(
data => {
console.log("data submitted");
},
err => console.log(err),
() =>{
console.log('Authentication Complete');
}
);
}
看到http://stackoverflow.com/a/39862337/3779853 – Blauhirn