I have Rest API that sends the csv String like
"aa,bb\n cc,dd" Here is the Rest Service code:
@GET @Path("/getCsv") @Produces({"text/csv"}) public String getCSV( ) { return "aa,ba,\n,ac,da"; } On UI side
Angular resource code to make get request code is as below:
angular.module('csvHelper', ['ngResource','configuration']).factory('csvHelper', function($resource,$rootScope){ return $resource(URL+'/api/getCsv/', {}, { query: {method:'GET', params:{}, cache :true} }); }); Code that makes get request and generate csv is as follows
csvHelper.query({}, function(response) { def.resolve(response); },function(response) { def.reject(response); }); var prom = def.promise; prom.then(function(text) { $window.open("data:text/csv;charset=utf-8," + encodeURIComponent(text)); }, function(error) { console.log('promise failed', error); }); In network tab I see rest API sending the data properly:
"aa,ba,\n,ac,da" But when I download the csv file, inside it I see
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object] What am I doing wrong.
EDIT
I tired to print response on console and this is what I saw:
[e { 0="a", $get=function(), $save=function(), more...}, e { 0="a", $get=function(), $save=function(), more...}, e { 0=",", $get=function(), $save=function(), more...}, e { 0="b", $get=function(), $save=function(), more...}, e { 0="a", $get=function(), $save=function(), more...}, e { 0=",", $get=function(), $save=function(), more...}, e { 0="\n", $get=function(), $save=function(), more...}, e { 0=",", $get=function(), $save=function(), more...}, e { 0="a", $get=function(), $save=function(), more...}, e { 0="c", $get=function(), $save=function(), more...}, e { 0=",", $get=function(), $save=function(), more...}, e { 0="d", $get=function(), $save=function(), more...}, e { 0="a", $get=function(), $save=function(), more...}] EDIT 2
I changed the logic to use $http like this:
$http.get("http://myurl.com/api/getcsv") .success(function (response) {console.log(success)}); I see the success printed on console but I don't see popup.
when I hit http://myurl.com/api/getcsv in browser I see popup to download file.
What am I missing
EDIT 3
This change worked.
$http.get(compiledUrl).then(function (response) { $window.open("data:text/csv;charset=utf-8," + encodeURIComponent(response.data)); },function(response){ console.log("error"); });
JSON.stringify(text). You should also useencodeURI()instead of encodeURIComponent()