Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Response is not an ArrayBuffer | unable to make api requests of responseType 'arraybuffer'

I'm getting this console error when trying to make a api request to download a pdf file.

service.ts

public getFileContent(fileURL: string): Observable<any> {
    return this.httpClient.get(fileURL, { responseType: 'arraybuffer'})
        .pipe(
            catchError(error => this.responseHandler.error(error, 'Error retrieving file content.'))
        );
}

component.ts

this.fileDownloadService
        .getFileContent(this.fileDetails.fileUid)
        .subscribe((res) => {
            const blob = new Blob([res], { type: 'application/pdf' });
            this.fileURL = this.sanitizer.bypassSecurityTrustResourceUrl(
                window.URL.createObjectURL(blob)
            );
        });

I have tried changing the responseType to 'blob' then I get this error 'Error: Response is not a Blob.'

any help would be highly appreciated. thank you.

this is the error message btw enter image description here

and api response in swagger is: enter image description here

like image 590
Luther Avatar asked Aug 07 '26 04:08

Luther


1 Answers

I ended up using fetch API to get the file with blob response type. Not sure why it didn't work with HttpClient.

fetch(fileURL).then(res => {
  const blob = await res.blob();
  this.iframeSrc = URL.createObjectURL(blob);
});
like image 124
Luther Avatar answered Aug 10 '26 14:08

Luther



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!