由于您使用的是Angular4,因此可以使用@ angular / common / http中的新HttpClient使用
Listening to progress
事件来实现.
const req = new HttpRequest('POST','/upload/file',file,{ reportProgress: true,});
接着,
http.request(req).subscribe(event => { // Via this API,you get access to the raw event stream. // Look for upload progress events. if (event.type === HttpEventType.UploadProgress) { // This is an upload progress event. Compute and show the % done: const percentDone = Math.round(100 * event.loaded / event.total); console.log(`File is ${percentDone}% uploaded.`); } else if (event instanceof HttpResponse) { console.log('File is completely uploaded!'); } });
编辑
由于OP希望将它与angular2一起使用,因此应该使用本机JavaScript XHR包装为此answer
中提到的Observable