<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<h1>Upload file</h1>
<h2>See result in byte array</h2>
<input type="file" onchange="readFile(this)" />
<p>Result as byte[] JSON.Stringify</p>
<textarea id="txtResult"></textarea>
</body>
</html>
// Code goes here
var readFile = function (files) {
var file = files.files[0];
var reader = new FileReader();
reader.onloadend = function () {
console.log('finished reading file');
encodeToByteArray(reader.result);
}
if (file) {
//reader.readAsDataURL(file);
reader.readAsArrayBuffer(file);
} else {
console.log('could not read file');
}
};
var encodeToByteArray = function (arrayBuffer) {
var byteArray = new Uint8Array(arrayBuffer);
var arr = [];
for (var i = 0; i < byteArray.byteLength; i++) {
// do something with each byte in the array
arr.push(byteArray[i]);
}
var arrString = JSON.stringify(arr);
document.getElementById("txtResult").value = arrString;
};