Created
December 9, 2015 18:20
-
-
Save justinwyllie/c83844bf41d8ab95f051 to your computer and use it in GitHub Desktop.
File Creation and Download and Re-upload without server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head lang="en"> | |
<meta charset="UTF-8"> | |
<title>Game data</title> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> | |
<script> | |
(function($) { | |
$(function() { | |
handleUpload = function (evt) { | |
var files = evt.target.files; | |
file = files[0]; | |
var reader = new FileReader(); | |
reader.onload = function (e) { | |
encapsulatedGameData = reader.result; | |
$('#result').html(encapsulatedGameData); | |
} | |
reader.readAsText(file); | |
}; | |
fileEl = $('#uploaded_game_data').get(0); | |
fileEl.addEventListener('change', function (e) { | |
handleUpload(e); | |
}); | |
$("#save_game_data").click(function () { | |
//TODO valid json? | |
//TODO it needs at least 4 entered chars | |
var game_data = $('#game_data').val(); | |
file = $("#file").get(0); | |
file.setAttribute("href", 'data:application/json,' + game_data); | |
$('#file').show(); | |
}) | |
}); | |
})(jQuery); | |
</script> | |
<style type="text/css"> | |
div * { | |
display: block; | |
font-family: arial,helvetica,sans-serif; | |
} | |
#file { | |
display: none; | |
} | |
</style> | |
</head> | |
<body> | |
<div> | |
<h3>Save your game data</h3> | |
<textarea id="game_data"></textarea> | |
<button id="save_game_data">Save</button> | |
<a id="file" src="" download="HexagonData.dat" width="200" height="100" border="0" style="border-width:0px">Get Your Game Data</a> | |
</div> | |
<hr> | |
<div> | |
<h3>Upload your game data</h3> | |
<input type="file" id="uploaded_game_data"> | |
<div id="result"> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment