AngularJs get request to download attached file

I have an http get request that retrieve from backend a txt file as attached. The response arrives correctly with correct data, but the txt attached downloaded has incorrect value : [object Object].
The code in the success block is:

var file = new Blob([data],{type: "text/plain"}); // data here contains correctly the values
var fileURL = URL.createObjectURL(file);
var a = document.createElement('a');
a.href = fileURL; = '_blank'; = idUser+'.txt';

What’s the correct way to see in the downloaded file the txt content?


here is solutions:

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Solution 1

Whenever you see a object unexpectedly transforming into "[object Object]" in JavaScript, it usually means that somewhere along the way, something that expected a string has implicitly typecast your object into one (the result of which is "[object Object]" ).

In this case, it’s because the Blob constructor (as documented here) expected an array of USVStrings, not an array of objects. So to properly stringify your object, rewrite your first line to:

var file = new Blob([JSON.stringify(data)],{type: "text/plain"});

That should solve your issue, but just as an additional tip, if this file is supposed to be human-readable at some point, you may want to call JSON.stringify with JSON.stringify(data, null, '\t') to prettify the file (see the third parameter of the JSON.stringify documentation for details).

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply