These are the only two methods currently available.
An issue with createObjectURL() is that it is deprecated and cannot be called in Chrome anymore.
Using FileSaver may feel like adding unnecessary complexity to your project when it should be a simple task in Angular.
This is where I am currently:
const params = {'params' : p};
const httpOptions = this.authenticationService.getRequestOptions();
httpOptions['responseType'] = 'text/csv';
this.http.post(
this.url + this.currentId,
params,
httpOptions
).subscribe((response: any) => {
console.log(response);
const blob = new Blob([response], {type: 'text/csv'});
const link = document.createElement('a');
link.setAttribute('style', 'display: none');
link.href = response; // should be a URI but document.URL.createObjectURL() is deprecated
const date = new Date();
link.download = 'file.csv';
link.click();
link.remove();
}, error => {
console.log('I failed :(', error);
});
The above code successfully downloads a file, but the issue of converting the blob to a URI remains unsolved.
Update for Clarity:
As of today, 19/11/2019, and since 1/11/2017, window.URL.createObjectURL is deprecated and will return an error. Click here for more information.
I came across this information:
More information on HTMLMediaElement.srcObject
Code examples related to srcObject
Discussion on deprecation of createObjectURL and introduction of HTMLMediaElement.srcObject
While it is supposed to replace createObjectURL
, it seems to work only for video elements.