Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 16, 2023 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/8c50e91bc89ec86d3169950e84242893 to your computer and use it in GitHub Desktop.
Save code-boxx/8c50e91bc89ec86d3169950e84242893 to your computer and use it in GitHub Desktop.
Javascript Read Files

JAVASCRIPT READ FILES

https://code-boxx.com/read-files-javascript/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>Read File</title>
<script>
function readFileA () {
// (A) GET SELECTED FILE
let selected = document.getElementById("demoPickA").files[0];
// (B) READ SELECTED FILE
let reader = new FileReader();
reader.addEventListener("loadend", () => {
document.getElementById("demoShowA").innerHTML = reader.result;
});
reader.readAsText(selected);
}
</script>
</head>
<body>
<input type="file" value="Choose TXT File" id="demoPickA"
accept="text/plain" onchange="readFileA()">
<div id="demoShowA"></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Read File</title>
<script>
function readFileB () {
// (A) GET SELECTED FILE
let selected = document.getElementById("demoPickB").files[0];
// (B) READ SELECTED FILE
let reader = new FileReader();
reader.addEventListener("load", () => {
let imgTag = document.getElementById("demoShowB");
imgTag.src = reader.result;
});
reader.readAsDataURL(selected);
}
</script>
</head>
<body>
<input type="file" value="Choose Image File" id="demoPickB"
accept="image/*" onchange="readFileB()">
<img id="demoShowB">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Read File</title>
<script>
function readFileC () {
// (A) GET SELECTED FILE
let selected = document.getElementById("demoPickC").files[0];
// (B) READ SELECTED FILE
let reader = new FileReader();
reader.addEventListener("load", () => {
var bindata = reader.result;
console.log(bindata);
});
reader.readAsBinaryString(selected);
}
</script>
</head>
<body>
<input type="file" value="Choose File" id="demoPickC"
onchange="readFileC()">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Read File</title>
<script>
function readFileD () {
// (A) GET SELECTED FILE
let selected = document.getElementById("demoPickD").files[0];
// (B) READ SELECTED FILE
let reader = new FileReader();
reader.addEventListener("load", () => {
var buffer = reader.result;
console.log(buffer);
console.log(buffer.byteLength);
});
reader.readAsArrayBuffer(selected);
}
</script>
</head>
<body>
<input type="file" value="Choose File" id="demoPickD"
onchange="readFileD()">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Pick File</title>
<script>
async function readFileE () {
// (A) OPEN FILE PICKER
[handler] = await window.showOpenFilePicker({
excludeAcceptAllOption: true,
types: [{
description: "Text",
accept: { "text/plain": [".txt", ".text"] }
}]
});
// (B) GET SELECTED FILE
let selected = await handler.getFile();
// (C) READ SELECTED FILE
let reader = new FileReader();
reader.addEventListener("loadend", () => {
document.getElementById("demoShowE").innerHTML = reader.result;
});
reader.readAsText(selected);
}
</script>
</head>
<body>
<input type="button" value="Choose File" onclick="readFileE()">
<div id="demoShowE"></div>
</body>
</html>
@davidmcmurrey2022
Copy link

Thanks. I'm trying to pick up just enough JavaScript to be able to read./write files. Been doing this for years with Perl!

David McMurrey, Ph.D.
Business, Government & Technical Communications
Austin Community College
https://mcmassociates.io/dmz_index.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment