Angular 9 файл загружен поврежден - PullRequest
0 голосов
/ 17 июня 2020

Я использую Angular 9 с HttpClient и File-Saver для загрузки файлов с сервера. Сейчас я пытаюсь загрузить файлы Excel

refindicateurService :

 downloadIndicateurById(indicateurCriteria: IndicateurCriteria): Observable<Blob> {
      console.log(indicateurCriteria);
      return this.authHttp.post( this.settings.server.url + `/indicateur/reportingIndicateur`, indicateurCriteria , {responseType: 'blob'})
            .pipe(map(res => res));
  }

Функция загрузки :

downloadFile(response: Response) {
    console.log(response);
    var blob = new Blob([this.getResponseBody(response)], { type: response.type });
    this.removeBusy();
    // let fileName: any = response.headers.get("etag");
    importedSaveAs(blob, 'data.xslx');
  }

Затем я называю это здесь:

exportIndicateurAsXls(id) {
    let indicateurCriteria = new IndicateurCriteria();
    indicateurCriteria.id = id;
    this.addBusy();
    this.refindicateurService.downloadIndicateurById(indicateurCriteria)
      .subscribe(response => {
        this.downloadFile(response);
        this.removeBusy();
      }, error => this.showError(error.status, JSON.parse(JSON.stringify(error)).message));
  }

Я получаю файл загружен, но поврежден при использовании 'arraybuffer' дает неопределенные данные

Исправить :

downloadFile(response: Blob) {
    this.removeBusy();
    importedSaveAs(response, 'data.xslx');
  }
...