Home Ask Login Register

Developers Planet

Your answer is one click away!

Michael February 2016

How do I create a download from a json structure in a scope variable

I currently just show the JSON in my template with

<pre>{{ mydata | json }}</pre>

I like to add a button for users to download the json data. Is there an easy way to do this ?


zoom February 2016

Downloading the JSON data, means downloading a file containing a JSON string. If you are already able to render that file on the server, then you just need to create an html link, with the href attribute pointing to the corresponding server url.

In order to force the file to be downloaded, you have to declared it as an attachment in an http header. The response must contains something like this:

'Content-Disposition: attachment; filename=data.json'

You can also specify the content-type with another response header:

'Content-Type': application/json

Edit: I just discovered that apparently you can initialize a file download directly from the client. See this SO topic. Seems to be pretty well supported: http://caniuse.com/#feat=bloburls

Post Status

Asked in February 2016
Viewed 2,346 times
Voted 4
Answered 1 times


Leave an answer

Quote of the day: live life