0

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 ?

Michael
  • 357
  • 5
  • 11

1 Answers1

1

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

Community
  • 1
  • 1
zoom
  • 1,686
  • 2
  • 16
  • 27
  • That does not work as the internal call is authenticated and has a Bearer token in the header. - If I just provide a link to the same api resource on the backend the authentication is lost and it returns an empty array. - I do need to keep the authentication in place. – Michael Feb 08 '16 at 21:02
  • See my edit then. Hope it will guide you through the process. – zoom Feb 08 '16 at 21:05