Hi you all,
I am an ng newbie, so maybe you would give me a clue on this.
I have a method which makes a Post to a remote api and receives a blob containg a file. It looks like this:
// …
public downloadExcel(): void {
const url: string = “[api endpoint here ]”;
this.http.post(url, data.body, { responseType: ‘blob’ }).subscribe((response: Blob) => {
saveAs(response, data.fileName + ‘.xlsx’);
);
}
//…
In this case, the filename is created inside the function. Everything is fine.
Now, I need to use the filename that is being returned by the api.
How can I achieve this?
Any help will be highly appreciated!
Txs in adv,
Ariel
You mean get fileName
from blob
? According to docs, blobs have only type and size. No name.
Tks for your reply.
I know that. But I know that I could get the filename from the headers sent by the server along with the blob. But I do not know how to do that= Or maybe is tposible using something else…
Works fine with node. But there are limitations in browsers (although I don’t know what Angular uses for fetching):
As you can see on the snippet I included, I am using the httpClient to make the post
I formatted your code a bit here:
public downloadExcel(): void {
const url: string = '[api endpoint here ]';
this.http.post(url, data.body, { responseType: 'blob' })
.subscribe((response: Blob) => saveAs(response, data.fileName + '.xlsx'));
}
This code isn’t valid JS, data.body
isn’t defined anywhere in this code, so it wont work.
The response: Blob
is fine, but I have no idea where, or what data.body
is.
I’d provide more insight to the code, a stackblitz reproduction of the code you want working, and the general setup/layout of everything usually helps
Oops, thank you very much for your time!
Yes, I did not pay attention to the function itself, but to the way it tries to work… My fault.
rActually, data is passed as a parameter and includes the payload.