2014-07-22 03:04:11 +08:00
|
|
|
<!DOCTYPE HTML>
|
|
|
|
<html>
|
|
|
|
<head>
|
2016-11-12 03:26:32 +08:00
|
|
|
<meta charset="UTF-8">
|
2016-10-28 15:36:05 +08:00
|
|
|
<title>Load and save | JSONEditor</title>
|
2015-02-28 04:54:04 +08:00
|
|
|
|
2015-03-01 03:47:23 +08:00
|
|
|
<script src="../dist/jsoneditor.js"></script>
|
2014-07-22 03:04:11 +08:00
|
|
|
|
2016-03-21 01:19:13 +08:00
|
|
|
<script src="https://bgrins.github.io/filereader.js/filereader.js"></script>
|
2016-02-09 17:05:11 +08:00
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2014-11-29/FileSaver.min.js"></script>
|
2014-07-22 03:04:11 +08:00
|
|
|
|
|
|
|
<style>
|
|
|
|
html, body {
|
|
|
|
font: 11pt sans-serif;
|
|
|
|
}
|
|
|
|
#jsoneditor {
|
|
|
|
width: 500px;
|
|
|
|
height: 500px;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>Load and save JSON documents</h1>
|
|
|
|
<p>
|
|
|
|
This examples uses HTML5 to load/save local files.
|
|
|
|
Powered by <a href="http://bgrins.github.io/filereader.js/">FileReader.js</a> and
|
|
|
|
<a href="https://github.com/eligrey/FileSaver.js">FileSaver.js</a>.<br>
|
|
|
|
Only supported on modern browsers (Chrome, FireFox, IE10+, Safari 6.1+, Opera 15+).
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
Load a JSON document: <input type="file" id="loadDocument" value="Load"/>
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
Save a JSON document: <input type="button" id="saveDocument" value="Save" />
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<div id="jsoneditor"></div>
|
|
|
|
|
2015-02-28 04:54:04 +08:00
|
|
|
<script>
|
2014-07-22 03:04:11 +08:00
|
|
|
// create the editor
|
2016-10-14 23:30:16 +08:00
|
|
|
var editor = jsoneditor(document.getElementById('jsoneditor'))
|
2014-07-22 03:04:11 +08:00
|
|
|
|
|
|
|
// Load a JSON document
|
|
|
|
FileReaderJS.setupInput(document.getElementById('loadDocument'), {
|
|
|
|
readAsDefault: 'Text',
|
|
|
|
on: {
|
|
|
|
load: function (event, file) {
|
2016-10-14 23:30:16 +08:00
|
|
|
editor.setText(event.target.result)
|
2014-07-22 03:04:11 +08:00
|
|
|
}
|
|
|
|
}
|
2016-10-14 23:30:16 +08:00
|
|
|
})
|
2014-07-22 03:04:11 +08:00
|
|
|
|
|
|
|
// Save a JSON document
|
|
|
|
document.getElementById('saveDocument').onclick = function () {
|
2016-06-04 01:32:21 +08:00
|
|
|
// Save Dialog
|
2016-10-14 23:30:16 +08:00
|
|
|
fname = window.prompt('Save as...')
|
2016-06-04 01:32:21 +08:00
|
|
|
|
|
|
|
// Check json extension in file name
|
2016-10-14 23:30:16 +08:00
|
|
|
if (fname.indexOf('.') == -1) {
|
|
|
|
fname = fname + '.json'
|
|
|
|
} else {
|
|
|
|
if (fname.split('.').pop().toLowerCase() == 'json'){
|
2016-06-04 01:32:21 +08:00
|
|
|
// Nothing to do
|
2016-10-14 23:30:16 +08:00
|
|
|
} else {
|
|
|
|
fname = fname.split('.')[0] + '.json'
|
2016-06-04 01:32:21 +08:00
|
|
|
}
|
|
|
|
}
|
2016-10-14 23:30:16 +08:00
|
|
|
var blob = new Blob([editor.getText()], {type: 'application/json;charset=utf-8'})
|
|
|
|
saveAs(blob, fname)
|
|
|
|
}
|
2014-07-22 03:04:11 +08:00
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
|
|
|
|
|