Question

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.

Was it helpful?

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

OTHER TIPS

You could use the HTML5 download attribute. It is not perfect and browser support

<!-- will download as "expenses.pdf" -->
<a href="/files/adlafjlxjewfasd89asd8f.pdf" download="expenses.pdf">Download Your Expense    Report</a>

Demo: http://davidwalsh.name/download-attribute

Support: http://caniuse.com/#feat=download

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top