FileReader() reading as Array Buffer

This commit is contained in:
ReznoRMichael 2021-01-22 22:45:39 +01:00
parent f4a6df4934
commit 31cc9aa611
2 changed files with 19 additions and 6 deletions

View file

@ -50,7 +50,7 @@
<!-- <button id="save-area-file" class="button" onclick="">Load Save</button> --> <!-- <button id="save-area-file" class="button" onclick="">Load Save</button> -->
<button id="save-area-read" class="button" onclick="HKReadTextArea();">Analyze</button> <button id="save-area-read" class="button" onclick="HKReadTextArea();">Analyze</button>
<input id="save-area-file" class="" type="file" name="file" accept=".dat,.bak*" onchange="ShowFile(this)"> <input id="save-area-file" class="" type="file" name="file" accept=".dat,.bak*" onchange="LoadSaveFile(this)">
<div class="checkboxes"> <div class="checkboxes">
<label for="checkbox-hints" class="checkbox-label" title="Check to see an optional clue for your first playthrough based on your save's progress."> <label for="checkbox-hints" class="checkbox-label" title="Check to see an optional clue for your first playthrough based on your save's progress.">

View file

@ -1,15 +1,14 @@
function ShowFile(input) { function ShowFile(input) {
let inputFileObject = input.files[0]; let inputFileObject = input.files[0];
let reader = new FileReader().readAsArrayBuffer(inputFileObject);
alert(`File name: ${inputFileObject.name}`);
// alert(`File name: ${inputFileObject.name}`); alert(`Last modified: ${inputFileObject.lastModified}`);
// alert(`Last modified: ${inputFileObject.lastModified}`);
alert(`File: ${inputFileObject}`);
} }
// main input tag file function // main input tag file function
function LoadSaveFile(input) { function LoadSaveFile(input) {
// Prepares a File object from the first file of the input files for reading as an Array Buffer
let inputFileObject = input.files[0];
// 1. read file // 1. read file
// The ArrayBuffer object is used to represent a generic, fixed-length raw binary data buffer. // The ArrayBuffer object is used to represent a generic, fixed-length raw binary data buffer.
@ -19,6 +18,20 @@ function LoadSaveFile(input) {
// readAsArrayBuffer(file) // readAsArrayBuffer(file)
// addEventListener("load", function) - to decode the file when loaded // addEventListener("load", function) - to decode the file when loaded
let inputReader = new FileReader();
inputReader.readAsArrayBuffer(inputFileObject);
inputReader.addEventListener("load", () => {
let inputArrayBuffer = inputReader.result;
try {
alert(`File: ${inputArrayBuffer}`);
}
catch (error) {
alert(`The file cannot be decoded. Error: ${error}`);
}
});
// 2. Decode file // 2. Decode file
// The slice() method copies up to, but not including, the byte indicated by the end parameter. // The slice() method copies up to, but not including, the byte indicated by the end parameter.