0

I am developing a single page app with Jquery. The user does some stuff on my app and I save some data in a JSON. Now I want to trasform the JSON in a text file and start the file download. How can I do this using only javascript, since I have no server side?

From what I understand, first step is encoding the JSON into base64:

    // Encode the text
    var text = btoa(JSON.stringify(json)));
    //create the file???

    //start the download??
Gian Luca Scoccia
  • 725
  • 1
  • 8
  • 26
  • you cannot access the client hard drive client-side with javascript. You'd need to use a different technology or a service/server-side code. – abc123 Feb 04 '14 at 15:12
  • @abc123: That's not entirely correct. – Cerbrus Feb 04 '14 at 15:16
  • If you want to let the user save it somewhere, check [this](http://stackoverflow.com/a/16198097/1282023) out. If you just want to make data accessible to your app, you might want to use [localStorage](https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage#localStorage). – MCL Feb 04 '14 at 15:21
  • @abc123: of course the user will have to authorize the download in the broswer, I just want pack the string into a file and start a download. – Gian Luca Scoccia Feb 04 '14 at 15:21
  • This is the best news ever! :) – abc123 Feb 04 '14 at 15:50

1 Answers1

2
document.location = 'data:Application/text,' + encodeURIComponent( JSON.stringify(data) );

Demo

Alexander
  • 12,424
  • 5
  • 59
  • 76