Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can JavaScript save to a local file?
    primarykey
    data
    text
    <p>There's already a solution for writing file JSON online but I want to save json file locally. I've tried to use this example <a href="http://jsfiddle.net/RZBbY/10/">http://jsfiddle.net/RZBbY/10/</a> It creates a link to download the file, using this call <code>a.attr('href', 'data:application/x-json;base64,' + btoa(t.val())).show();</code> Is there a way to save the file locally instead of providing a downloadable link? There are other types of conversion beyond <code>data:application/x-json;base64</code>?</p> <p>Here's my code:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;head&gt; &lt;meta charset="utf-8"&gt; &lt;title&gt;jQuery UI Sortable - Default functionality&lt;/title&gt; &lt;link rel="stylesheet" href="http://jqueryui.com/themes/base/jquery.ui.all.css"&gt; &lt;script src="http://jqueryui.com//jquery-1.7.2.js"&gt;&lt;/script&gt; &lt;script src="http://jqueryui.com/ui/jquery.ui.core.js"&gt;&lt;/script&gt; &lt;script src="http://jqueryui.com/ui/jquery.ui.widget.js"&gt;&lt;/script&gt; &lt;script src="http://jqueryui.com/ui/jquery.ui.mouse.js"&gt;&lt;/script&gt; &lt;script src="http://jqueryui.com/ui/jquery.ui.sortable.js"&gt;&lt;/script&gt; &lt;script src="http://jqueryui.com/ui/jquery.ui.accordion.js"&gt;&lt;/script&gt; &lt;link rel="stylesheet" href="http://jqueryui.com/demos/demos.css"&gt; &lt;meta charset="utf-8"&gt; &lt;style&gt;a { font: 12px Arial; color: #ac9095; }&lt;/style&gt; &lt;script type='text/javascript'&gt; $(document).ready(function() { var f = $('form'), a = $('a'), i = $('input'), t = $('textarea'); $('#salva').click(function() { var o = {}, v = t.val(); a.hide();//nasconde il contenuto i.each(function() { o[this.name] = $(this).val(); }); if (v === '') { t.val("[\n " + JSON.stringify(o) + " \n]") } else { t.val(v.substr(0, v.length - 3)); t.val(t.val() + ",\n " + JSON.stringify(o) + " \n]") } }); }); $('#esporta').bind('click', function() { a.attr('href', 'data:application/x-json;base64,' + btoa(t.val())).show(); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form&gt; &lt;label&gt;Nome&lt;/label&gt; &lt;input type="text" name="nome"&gt;&lt;br /&gt; &lt;label&gt;Cognome&lt;/label&gt; &lt;input type="text" name="cognome"&gt; &lt;button type="button" id="salva"&gt;Salva&lt;/button&gt; &lt;/form&gt; &lt;textarea rows="10" cols="60"&gt;&lt;/textarea&gt;&lt;br /&gt; &lt;button type="button" id="esporta"&gt;Esporta dati&lt;/button&gt; &lt;a href="" style="display: none"&gt;Scarica Dati&lt;/a&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload