Created
September 25, 2022 01:39
-
-
Save emchateau/73f2e8dc5252b598b05fc443f532c39d to your computer and use it in GitHub Desktop.
Load local file with JS
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
<html> | |
<body> | |
<form id="jsonFile" name="jsonFile" enctype="multipart/form-data" method="post"> | |
<fieldset> | |
<h2>Json File</h2> | |
<input type='file' id='fileinput'> | |
<input type='button' id='btnLoad' value='Load' onclick='loadFile();'> | |
</fieldset> | |
</form> | |
<script type="text/javascript"> | |
function loadFile() { | |
var input, file, fr; | |
if (typeof window.FileReader !== 'function') { | |
alert("The file API isn't supported on this browser yet."); | |
return; | |
} | |
input = document.getElementById('fileinput'); | |
if (!input) { | |
alert("Um, couldn't find the fileinput element."); | |
} | |
else if (!input.files) { | |
alert("This browser doesn't seem to support the `files` property of file inputs."); | |
} | |
else if (!input.files[0]) { | |
alert("Please select a file before clicking 'Load'"); | |
} | |
else { | |
file = input.files[0]; | |
fr = new FileReader(); | |
fr.onload = receivedText; | |
fr.readAsText(file); | |
} | |
function receivedText(e) { | |
let lines = e.target.result; | |
var newArr = JSON.parse(lines); | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment