Created
September 6, 2016 13:34
-
-
Save scoobster17/d78f2ccb6066143999fdc1bdefc63210 to your computer and use it in GitHub Desktop.
Read file from web client with 'upload', not AJAX
This file contains hidden or 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> | |
<title>Read file test</title> | |
</head> | |
<body> | |
<form> | |
<h1>Read file test</h1> | |
<label for="file">File to read:</label> | |
<br /> | |
<input type="file" name="file" id="file"> | |
<br /> | |
<br /> | |
<input type="submit" value="Submit"> | |
</form> | |
</body> | |
<script type="text/javascript"> | |
var form = document.getElementsByTagName('form')[0]; | |
var input = document.getElementById('file'); | |
function submit(e) { | |
// prevent form submission | |
e.preventDefault(); | |
// create file reader instance | |
var fileReader = new FileReader(); | |
// handle behaviour on load | |
fileReader.onload = function() { | |
// parse and log the content as JS object | |
var obj = JSON.parse(fileReader.result); | |
console.log(obj); | |
} | |
// request and read file | |
fileReader.readAsText(input.files[0]); | |
// make sure form doesn't submit | |
return false; | |
} | |
window.addEventListener('submit', submit); | |
</script> | |
</html> |
This file contains hidden or 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
{ | |
"name": "test" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment