首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角10可观测多次响应

角10可观测多次响应
EN

Stack Overflow用户
提问于 2022-03-15 19:59:30
回答 1查看 136关注 0票数 0

我有一个功能,在我的角10应用程序,我创建一个blob和上传一个文件。当我订阅可观察到的内容时,它会回复五次。我不知道在哪里或者为什么?

代码语言:javascript
复制
uploadBacklogUploadFile(data, fileName: string) {
    const blob = new Blob([data], { type: 'text/csv' });
    this.backlogUploadService
      .uploadBacklogFile(blob, fileName, this.selectedJoin.id)
      .subscribe(        
        () => {  <-----this block of code will run 5 times
          this.uploadPercent = 100;
          this.dialogRef.close(true);
          this.openDialog();
          this.backlogUploaded.emit(true);
        },
        () => {
          this.uploadPercent = 0;
          this.snackBar.open('The backlog failed to be uploaded', null, {
            panelClass: 'snack-bar-error'
          });
        },
      );
  }

还有我的服务

代码语言:javascript
复制
  public uploadBacklogFile(file: string | Blob, fileName: string, Id: number): Observable<HttpEvent<any>> {
    const requestView = new BacklogUploadRequestView(Id);
    const formData: FormData = new FormData();
    formData.append('backlogUpload', new Blob([JSON.stringify(requestView)], { type: 'application/json' }), '');
    formData.append('file', file, fileName);
    const headers = new HttpHeaders({
      Accept: 'application/json',
    });
    const url = this.remoteClientUtils.getThorUrl() + `${this.BACKLOG_UPLOAD_URL}`; 
    console.log("checkpoint") <------- this will only log once, so it must be nothing above?
    return this.httpClient.request(HttpMethods.POST, url, {
      body: formData,
      headers: headers,
      reportProgress: true,
      observe: 'events'
    })
    .pipe(
      catchError(this.remoteClientUtils.handleError<HttpEvent<any>>(null)));
  }
EN

回答 1

Stack Overflow用户

发布于 2022-03-15 20:04:33

您试图跟踪上载的进度,当您使用reportProgressobserve (https://angular.io/guide/http#requesting-data-from-a-server)时,我认为您应该删除这些选项

代码语言:javascript
复制
this.httpClient.request(HttpMethods.POST, url, {
   body: formData,
   headers: headers,
   reportProgress: true, //delete this
   observe: 'events'    // and delete this
})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71488311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档