I am currently working on a task to securely download files (PDF/excel/text) using an API in my Angular 2 (beta) system.
Initially, I utilized a post API with an authentication header and attempted to create a blob from the received data bytes.
The code snippet I used was:
return this.http.get(url, { headers: this.headers}).map( response => response.blob())
However, I encountered an error indicating that the blob method is not implemented in Angular 2 HTTP.
Subsequently, I experimented with the following code where I needed to convert a string to a byte array:
return this.http.get(Configuration.API_URL + url, { headers: this.headers }).map(
response => {
var bytes = [];
var utf8 = encodeURIComponent(response.text());
for (var i = 0; i < utf8.length; i++) {
bytes.push(utf8.charCodeAt(i));
}
var data = new Blob([bytes], { type: 'application/pdf' });
var fileURL = URL.createObjectURL(data);
window.open(fileURL);
}
);
Unfortunately, I am encountering some issues with the byte array as it does not match the one provided by the API.
I require assistance either in converting a string to a byte array or efficiently utilizing blobs in an Angular 2 HTTP get request.