繁体   English   中英

如何等待 http 响应 angular

[英]how to wait for http response angular

我试图上传一个文件,读取它然后重定向到一个组件,但它没有读取我尝试等待但我不知道把它放在哪里或者是否有更多解决方案的文件

零件

 upload() {
 this.progress.percentage = 0; 
this.currentFileUpload = this.selectedFiles.item(0);
this.uploadService.pushFileToStorage(this.currentFileUpload).subscribe(event => {
  if (event.type === HttpEventType.UploadProgress) {
    this.progress.percentage = Math.round(100 * event.loaded / event.total);
  } 
  else  if (event.type === HttpEventType.Response) {
   
  let hud: Hud
  hud =event.body as Hud
  for (let i =0; i<this.dataService.huds.length; i++ ){
  if (this.dataService.huds[i].nombre_mesa===hud.nombre_mesa){
this.dataService.huds.splice(i,1)
}
}
this.dataService.huds.push(event.body as Hud)




  }       
});

this.selectedFiles = undefined;
}

服务

pushFileToStorage(file: File): Observable<HttpEvent<{}>> {
let formdata: FormData = new FormData();

formdata.append('file', file);

const req = new HttpRequest('POST', 'http://localhost:8080/post', formdata, {
  reportProgress: true,
  responseType: 'json'
});

return this.http.request(req);
}

你可以添加你的逻辑完成这个回调是在动作响应之后调用

this.uploadService.pushFileToStorage(this.currentFileUpload).subscribe({
    next: value => console.log(value),
    error: err => console.error(err),
    complete: () => console.log('DONE!')
});
pushFileToStorage(file).subscribe(event =>

switch (event.type) {
  case HttpEventType.UploadProgress:
    console.log('upload progression percentage: ', Math.round(event.loaded * 100 / event.total));
  break;
  case HttpEventType.Response:
    // upload complete.
  break;
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM