60 lines
No EOL
1.8 KiB
JavaScript
60 lines
No EOL
1.8 KiB
JavaScript
function ShowFile(input) {
|
|
let inputFileObject = input.files[0];
|
|
|
|
alert(`File name: ${inputFileObject.name}`);
|
|
alert(`Last modified: ${inputFileObject.lastModified}`);
|
|
}
|
|
|
|
// main input tag file function
|
|
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
|
|
// The ArrayBuffer object is used to represent a generic, fixed-length raw binary data buffer.
|
|
// It is an array of bytes, often referred to in other languages as a "byte array".
|
|
|
|
// new FileReader()
|
|
let inputReader = new FileReader();
|
|
// readAsArrayBuffer(file)
|
|
inputReader.readAsArrayBuffer(inputFileObject);
|
|
// addEventListener("load", function) - to decode the file when loaded
|
|
inputReader.addEventListener("load", FileObjectToArrayBuffer);
|
|
|
|
// 2. Decode file
|
|
|
|
// base64 Decoding (ArrayBuffer)
|
|
// AES decryption (ECB) removes pkcs7 padding (ArrayBuffer)
|
|
// Convert ArrayBuffer to string/text TextDecoder().decode(ArrayBuffer)
|
|
|
|
// 3. Convert file to JSON string (optional?)
|
|
// JSON.stringify(JSON.parse(decrypted), undefined, 2)
|
|
|
|
// 4. Paste decoded string file to text area
|
|
}
|
|
|
|
// reads the File object as an Array Buffer
|
|
function FileObjectToArrayBuffer() {
|
|
let inputArrayBuffer = this.result;
|
|
|
|
try {
|
|
// Uint8Array(ArrayBuffer)
|
|
inputArrayBuffer = new Uint8Array(inputArrayBuffer);
|
|
|
|
// ArrayBuffer.slice()
|
|
// The slice() method copies up to, but not including, the byte indicated by the end parameter.
|
|
inputArrayBuffer.slice();
|
|
|
|
// remove C# header (ArrayBuffer)
|
|
|
|
|
|
alert(`File: ${inputArrayBuffer}`);
|
|
}
|
|
catch (error) {
|
|
alert(`The file cannot be read. Error: ${error}`);
|
|
}
|
|
}
|
|
|
|
function RemoveCSharpHeader() {
|
|
|
|
} |