Skip to content

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

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(,fileId).subscribe(response => {
      const a = document.createElement('a')
      const objectUrl = URL.createObjectURL(response)
      a.href = objectUrl = response;;

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 ???


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

to = 'fil1.jpg';

Then you will get the correct file name.