2

I made the service side to return csv format when I call the service with $http Get with Angular if the user click the button..

So the return is like 1,2,3,4,5,6,7,8,9,10

What is the best way to give the user the way to save it their local machine?

5
  • 1
    if your server is already creating the data in a format that is acceptable to your user, can the button just forward them to that URL (making sure that the mime type is correct, of course!) Commented Nov 1, 2012 at 0:51
  • 2
    You're best off pushing the download to the user using the server. What server architecture do you have (eg PHP or Java or Node) Commented Nov 1, 2012 at 3:02
  • But the tricky thin is that the service can only accept POST.. If the service can accept GET, there is no problem to redirect the user.. But How can I do it with POST? Commented Nov 1, 2012 at 5:35
  • 1
    @Rok First you are talking about GET, now about POST. What are you looking for? I also don't get why you need to retrieve the CSV if its anyway just for downloading? Simply add a link to your API (or whatever the CSV comes from) and you are done, aren't you? Commented Nov 1, 2012 at 10:28
  • Yep.. I was confused.. Forward traffic to API directly will solve my problem... thanks :) Commented Nov 6, 2012 at 22:51

2 Answers 2

6

There are two options

1) Send the csv from your server.

2) Data URIs. I think this is what you're asking for. Here is an example

/**
 * @param{string} content The contents of a file to download.
 * @param{string} fileName The name to save the file as on the user's computer.
 */
function(content, fileName) {
  var dataUrl = 'data:text/csv;utf-9,' + encodeURI(content);
  var link = document.createElement('a');
  angular.element(link)
    .attr('href', dataUrl)
    .attr('download', fileName) // Pretty much only works in chrome
  link.click();
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can try Alasql library, which can save data in CSV format one line:

 function myCtrl($scope) {
    $scope.filename = "mydata.csv";
    $scope.cities = [{city:"Odessa",population:100000},
                  {city:"Voronezh",population:201022},
                  {city: "Paris", population: 3000000}];

    $scope.saveCSV = function(filename, content) {
          alasql('SELECT city, population INTO CSV("'+$scope.filename+'",{headers:true}) FROM ?',
           [$scope.cities]);
    };
 };

See the full example code in jsFiddle.

In this example Alasql use SELECT operator to choose required fields from data array (AngularJS' creates $$hashKey field, which probably is not needed to users). If you do not use on these data simply export to CSV file with:

alasql('SELECT * INTO CSV('mydata.csv') FROM ?',[$scope.data]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.