簡體   English   中英

如何從angular中的文件上傳服務中提取響應數據

[英]how to extract response data from the file upload service in angular

大家好,每個人都在開發上傳功能,我們將上傳 excel,我將得到我想要提取的響應

我的 API 服務

  uploaduserConfigData(files) {
let token = window.sessionStorage.getItem('jwt');
const URL = 'http://localhost:8080/upload';
const headers = new Headers({'Content-Type': 'application/json'});
headers.append('Authorization', 'Bearer ' + token);
headers.append('Tenant', 'demoDB');

return this.http.post(URL, files, {
  reportProgress: true,
  observe: 'events',
  headers: new HttpHeaders().set('Authorization', 'Bearer ' + token).set('Tenant', 'demoDB')
})
  .pipe(
    catchError(this.handleError)
  );

}

我得到的響應樣本

回復截圖

{
    "total": 12,
    "repeatedUsers": 1,
    "emailIdError": 1,
    "mobileNoError": 12,
    "designationError": 0,
    "subjectError": 12,
    "fileS3Url": "https://s3.ap-south-1.amazonaws.com/excel/newExcel.xls"
}

但是當我嘗試從 JSON 響應中提取總數時,它給出了一個未定義的錯誤

this.apiService.upload(formData).subscribe((data: any) => {
        if (data.type === HttpEventType.UploadProgress) {
          this.progress = Math.round((100 / files.total) * files.loaded);
          console.log('progress' + this.progress);
        } else if (files.type === HttpEventType.Response) {
          this.progress = null;
        }

        this.userconfigdata = JSON.stringify(data);

        alert(JSON.stringify(this.userconfigdata.total));

      }, error => {
        console.log(error);
      });

未定義的響應

this.userconfigdata已經使用JSON.stringify()序列化。 當您說this.userconfigdata.total時,沒有可用的屬性total 嘗試替換語句

alert(JSON.stringify(this.userconfigdata.total));

alert(JSON.stringify(data.total));

或者

將 object 分配給成員變量,無需序列化並訪問它的屬性。

this.userconfigdata = data;
alert(JSON.stringify(this.userconfigdata.total));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM