How can JavaScript save to a local file?

后端 未结 7 1303
渐次进展
渐次进展 2020-12-29 08:27

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 http://jsfiddle.net/RZBbY/10/ It creates a lin

7条回答
  •  独厮守ぢ
    2020-12-29 09:12

    Based on http://html5-demos.appspot.com/static/a.download.html:

    var fileContent = "My epic novel that I don't want to lose.";
    var bb = new Blob([fileContent ], { type: 'text/plain' });
    var a = document.createElement('a');
    a.download = 'download.txt';
    a.href = window.URL.createObjectURL(bb);
    a.click();
    

    Modified the original fiddle: http://jsfiddle.net/9av2mfjx/

提交回复
热议问题