在我用 Angular 4 开发的应用程序中,用户可以将多部分文件上传到服务器。文件很大。我需要向用户显示文件上传过程的当前进度及其百分比,我该怎么做?
提前致谢!
这适用于 Angular 9 和 10 (note observe: 'events'
)
const headers = new HttpHeaders({
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: token
}))
const formData = new FormData();
formData.append('file_param_name', file, file.name);
this.httpClient.post(url, formData, {
headers,
reportProgress: true,
observe: 'events'
}).subscribe(resp => {
if (resp.type === HttpEventType.Response) {
console.log('Upload complete');
}
if (resp.type === HttpEventType.UploadProgress) {
const percentDone = Math.round(100 * resp.loaded / resp.total);
console.log('Progress ' + percentDone + '%');
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)