Search code examples
jsonrestangularjsdownloadsitebricks

How to make browser download json returned from rest service


The frameworks i'm using are AngularJS(client-side) and Sitebricks(server-side). I want to return a list of objects into json and make the client download that json by prompting the user where he wants to save the file.

I accept all kind of propositions. Cheers in advance.


Solution

  • It sounds like you want to create the file in the browser, and save it to the user's local machine. One method is presented here, but it doesn't work in IE Javascript: Create and save file

    Here's a fiddle: http://jsfiddle.net/vUdyD/

    $(function() {
      function exportToCsv() {
            var myCsv = "Col1,Col2,Col3\nval1,val2,val3";
    
            window.open('data:text/csv;charset=utf-8,' + escape(myCsv));
        }
    
        var button = document.getElementById('b');
        button.addEventListener('click', exportToCsv);
    });