7

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.

sachin kulkarni
  • 737
  • 1
  • 13
  • 26
  • Yes, blob() is coming soon. Meanwhile, please check this out: http://stackoverflow.com/questions/35368633/angular-2-download-pdf-from-api-and-display-it-in-view – mico Jul 29 '16 at 10:42
  • Thankyou mico this xhr answer helped me – sachin kulkarni Aug 02 '16 at 10:19
  • What is the difference between the byte arrays ? all elements or only header/footer ? – Nicolas Henneaux Aug 05 '16 at 05:30
  • I have used this module: http://alferov.github.io/angular-file-saver/ for downloading excel files. Let me know if you need to see the actual code i have written. – sfs Aug 05 '16 at 10:05

1 Answers1

1

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

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

Hope it helps you!

Cheers!

Varit J Patel
  • 3,497
  • 1
  • 13
  • 21