Alex Rivera | Logout

How to write localStorage data to a text file in Chrome

Asked 2012-01-01T13:21:32.533
8

I want to write a localStorage item to a text file and want to invoke user to store the file in a specified location. Please help me with extending the code

  var data = JSON.parse(localStorage.getItem(pid));   
  var Text2Write = "";
  for(var pr in ele)
  {
     var dat = pr + ":" + ele[pr] + "\n";
     Text2Write += dat;
  }
  // Here I want to store this Text2Write to text file and ask user to save where he wants.  

Please help me extending this code.

Edit
Report

1 Answer

4

If you don't want to use a server-side solution (does not have to be PHP of course), the easiest way is to use a data URI:

data:text/plain,Your text here

This will show the text file in the browser, and let the user save it where they want. I don't think it's possible to show a "Save as"-dialog for those kind of URI:s.

Note: this requires IE8 at least. But I'm guessing that's your requirement anyway, since you are using localStorage.

answered 2012-01-01T13:44:05.213

Your Answer