HTML:
<div>
<label for="file">选择文件</label>
<input type="file" id="file" (change)="postFile($event.target.files)">
</div>
ts:
public postFile(files: FileList){
this.fileToUpload = files[0];
// console.log(this.fileToUpload); 可以获取到图片的大小,格式等信息
const formData = new FormData();
formData.append( 'file', this.fileToUpload);
// const httpOptions = {
// headers:new HttpHeaders({
// 'Content-Type':'application/json',
// })
// }
let imgUrl = "api/empImg"
this.http.post(imgUrl, formData).subscribe((data:baseinit)=>{ // post发送图片
console.log(data.message)
})
}