convert string/Text to byte array in type script

13,581

Probably, The below npm package can help you convert it into ByteArray.

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

Hope it helps you!

Cheers!

Share:
13,581
sachin kulkarni
Author by

sachin kulkarni

I am an Indian web developer/programmer, based in Pune. I had spent Couple of years years in the corporate world as a website developer. I feel I have been successful, not just because of my web programming skills, but because I am a reliable and dependable web programmer that can be trusted to follow through with my work.

Updated on July 06, 2022

Comments

  • sachin kulkarni
    sachin kulkarni almost 2 years

    I am Currently working on task to download file (PDF/excel/text) using secure API in my system in angular 2 (beta) version.

    I have used post API with authentication header and trying to create blob using data bytes received.

    I have tried using following code

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

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

    so i am trying following code where i need to convert string to 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);
                }
            );
    

    here i am facing some issue with the bytes array. Byte array is not same as the one sent by the API.

    Need help in either converting string to byte array or using blob in angular 2 HTTP get request.