How to save a large json object to file? - angularjs

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.

Related

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.

angularjs: download REST API response as CSV file

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.

How to upload an image in angular as an API client?

I'm using Angular as a client to a Rails backend. I'd like to have a form that can accept multipart upload data, and pass it to the server. But the only data I get on the files is:
{"webkitRelativePath"=>"",
"lastModified"=>1424644319000,
"lastModifiedDate"=>"2015-02-22T22:31:59.000Z",
"name"=>"imgres.jpg",
"type"=>"image/jpeg",
"size"=>7521}
Shouldn't this contain something else? I need the actual binary data. How do I get this?
there is a module on file upload called angular-file-upload check it out
hope this help you..!

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.

capture and save string to local file

I have a very simple app with which I want to capture a string and save it to a file within the app directory. Is it possible to use $http to post/put to a file within the app tree similar to how you'd get static files like views?
Thanks
C
Use HTML5 filesystem API. Example here.

Resources