1.首先在app.module里面引入 HttpClientModule
import {HttpClientModule} from '@angular/common/http'
2.在页面里面引用
import {HttpClient,HttpRequest,HttpHandler} from '@angular/common/http';
constructor(private httpclient:HttpClient) { }
/**
* 上传事件
*/
upload(){
const url = ''; //后台接口
var formData = new FormData();
formData.append('uploadFile',this.Imgfile); //uploadFile 需要和后台字段对应
formData.append('name',this.name);
const req = new HttpRequest('post',url,formData,{
reportProgress:true,
})
this.httpclient.request(req).subscribe(val => {
console.log(val)
})
}