Convert string / text to byte array in script type

I am currently working on the task of downloading a file (PDF / excel / text) using a secure API on my system in angular 2 (beta).

I used the post API with an authentication header and tried to create a blob using the received bytes of data.

I tried using the following code

return this.http.get(url, { headers: this.headers}).map( response => response.blob()) 

But, I got an error that the blob method is not implemented in angular 2 HTTP.

so I am trying to use the following code where I need to convert a string to an array of bytes.

 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); } ); 

here I ran into a byte problem. The byte array is not the same as the one sent by the API.

Need help converting a string to an array of bytes or using blob in an angular 2 HTTP request.

+6
source share
1 answer

Perhaps the npm package below can help you convert it to ByteArray.

https://www.npmjs.com/package/xdata

Hope this helps you!

Hooray!

+1
source

All Articles