angularjs: download REST API response as CSV file - angularjs

I have a REST API whose response json (array of objects).
I would like to provide a button in my view like "Download results to CSV" and
and when user clicks on the link csv file should be downloaded with comma separated values.
What is best way to achieve this in angularjs?

Check out this fiddle (not the author):
Fiddle
You can either implement that on your client side, or alternatively on your server side with a new API call to get the CSV file directly.

Related

Import data from a CSV file from Angular

I want to import data from a csv file and insert them in MongoDB. So firstly I get the data from the csv file and I display my data in the console using AngularJS.
Now I want to pass my data (from AngularJS) to a play controller where I want to parse data and then insert it in my database.
So I defined this route:
POST /input/:data controllers.InputController.showinput(data)
Where data is the data that I get from AngularJS and InputController is my controller.
I am currently blocked, I don't know if I'm on the right track or not and also there is any example to parse my data in controller?
Instead of getting data from csv file, you can first insert csv file in mongoDB by using the monngoImport command. Then you can access data from csv file using angularjs.

File download angular and Laravel

I am trying to download a pdf file generated by laravel in AngularJs. So far i have been able to get the file from laravel but when i click the action, Angular does not download the file but just returns a response that includes the file as in the diagram below.
Response from laravel returns pdf file
How do i get the file to download?
Laravel
public function getPrintInvoice()
{
$pdf = PDF::loadView('crystal.invoice');
return $pdf->download('hello.pdf');
}
Angular
$http.get('print-invoice');
Because $http.get() uses an asynchronous call to the server the response is captured by the XMLHttpResponse object rather than presented directly to the user. The easiest method of presenting the PDF to the user is to skip the angular call, e.g. use $location.url('/print-invoice'); or $window.open('/print-invoice');
If you want to stick with the $http methods (e.g. if you want to be able to perform some action based on the success or failure of the file get) you'll need to capture the return value from the download and use the HTML5 Blob methods. I'd recommend using something like Angular File Saver. But it does require a bit more work on the server side to ensure the data is in the correct format.

Building a multipart form with ng-admin to post an attribute and file data to the server

Using ng-admin, how can you build a creation view form with two fields: a name, and a file?
The file field type lets you easily select a file as part of a form, but it immediately posts it to an endpoint that you configure. That endpoint returns the created filename, which you can then refer to when you submit the form.
This strategy doesn't work for me. I need ng-admin to let me select a file, enter a name for the file, and then submit both pieces of data as a multipart encoded POST when I submit the form.
I think this might be possible with some clever uses of custom Angular directives, but I'm not sure about the right way to go about it. Any ideas?

How to save a large json object to file?

Just started with angular and I am debugging my angular app and would like to save a large json object. Is it possible to save this in chrome? Or how to save this with angular?
Extra info: I want to save the contents of $rootScope to a json file.
Not sure from your question where you want to save the file so I'll assume server side you could post the json to a rest service using $post (a get request will probably be too long) on the server that writes the file with the posted contents.
You should use the browser tools from your favorite browser. in your code write this code:
// ..
var json = JSON.stringify($rootScope);
console.log(json);
You will be able to copy the content from the console and you can save it into a new text file.

Angularjs: write json data to file

Simple question, is it possible to write json data (received from API) to a json file in my directory structure. The api returns a json string. Is there any method in angular that supports this or do I need to rely on jquery? Extra information on this topic is welcome.
You cannot access the user's filesystem from javascript, instead you should present the user with a save dialog (it's up to the user where to save the file). AngularJS doesn't support this out of the box, you could however save a file to the default download folder by converting the JSON to a base64 string and bind it to an a href.
<a href="data:application/octet-stream;charset=utf-16le;base64,ew0KICAgICJnbG9zc2FyeSI6IHsNCiAgICAgICAgInRpdGxlIjogImV4YW1wbGUgZ2xvc3Nhcnki
IA0KICAgIH0NCn0=">json file</a>
For more control to save files to the client, you could use FileSaver which has support for older browsers.

Resources