目前,我想将文件上传到我们的服务器.我可以做到这一点 的Http从@angular/http库.
但现在,我们希望使用HttpClient进行更改以与当前项目保持一致.我做了搜索,我找不到如何使用HttpClient将表单数据添加到post请求的解决方案?
[更新]我用来通过http上传文件的代码
let formData: FormData = new FormData();
formData.append('file', file, file.name);
this._http.post(base_api_url + uploadFileApi, formData)
有人可以帮助我吗?
非常感谢.
Bab*_*bak 16
这是Angular服务:
import { Injectable } from '@angular/core';
@Injectable()
export class UploadService {
constructor(private http: HttpClient) {}
uploadFile(file: File): Observable<any> {
const formData = new FormData();
formData.append('file', file);
const headers = new HttpHeaders({ 'enctype': 'multipart/form-data' });
return this.http.post(apiUrl, formData, { headers: headers });
}
}
Run Code Online (Sandbox Code Playgroud)
可以这么简单地写。其中path是请求路径,data是您的表单数据。
constructor(private httpClient: HttpClient) {
this.httpTimeout = 90000;
this.retryCount = 1;
}
method(path: string, data: any) {
this.httpClient.post(path, data).retry(retry).timeout(timeout);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9496 次 |
| 最近记录: |