i want to download file from server in angular :
this code from service:
JavaScript
x
6
1
DownloadVerifyFile(requestId, fileId): any {
2
return this.httpClient
3
.get(this.appConfig.apiEndpoint + '/VerifyRequest/File/' + requestId + '/' + fileId,
4
{ responseType: 'blob' });
5
}
6
and this code for download that file in brwoser:
JavaScript
1
11
11
1
DownloadFile(fileId): void {
2
this.requestService.DownloadVerifyFile(this.id,fileId).subscribe(response => {
3
const a = document.createElement('a')
4
const objectUrl = URL.createObjectURL(response)
5
a.href = objectUrl
6
a.download = response;
7
a.click();
8
URL.revokeObjectURL(objectUrl);
9
});
10
}
11
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 ???
Advertisement
Answer
Because you have named the file by response(It is an object). You were almost achieved. Just a little change as following:
JavaScript
1
2
1
a.download = response;
2
to
JavaScript
1
2
1
a.download = 'fil1.jpg';
2
Then you will get the correct file name.