change file name in angular when download that by “Blob”

Tags: , ,



i want to download file from server in angular :

this code from service:

  DownloadVerifyFile(requestId, fileId): any {
    return this.httpClient
        .get(this.appConfig.apiEndpoint + '/VerifyRequest/File/' + requestId + '/' + fileId,
            { responseType: 'blob' });
}

and this code for download that file in brwoser:

 DownloadFile(fileId): void {
    this.requestService.DownloadVerifyFile(this.id,fileId).subscribe(response => {
      const a = document.createElement('a')
      const objectUrl = URL.createObjectURL(response)
      a.href = objectUrl
      a.download = response;
      a.click();
      URL.revokeObjectURL(objectUrl);
    });
  }

but i have a problem with that , when i downlaod file , file name is this [object Blob] but i want to download by orginal name for example if file is file1.jpg , when downloaded file name must be fil1.jpg not [object Blob] . how can i solve this problem ???

Answer

Because you have named the file by response(It is an object). You were almost achieved. Just a little change as following:

a.download = response;

to

a.download = 'fil1.jpg';

Then you will get the correct file name.



Source: stackoverflow