-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
66 lines (60 loc) · 2.15 KB
/
utils.js
File metadata and controls
66 lines (60 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
///////////////////////////////////////////////////////////////////////////////
/// @file utils.js
///
/// @brief Utility functions for the MinZX 48K Spectrum emulator
///
/// @author David Crespo Tascon
///
/// @copyright (c) David Crespo Tascon
/// This code is released under the MIT license,
/// a copy of which is available in the associated LICENSE file,
/// or at http://opensource.org/licenses/MIT
///////////////////////////////////////////////////////////////////////////////
"use strict";
// load from remote URL as Uint8Array passed to callback
function loadRemoteBinaryFile(url, callback)
{
let req = new XMLHttpRequest();
req.responseType = 'arraybuffer';
req.addEventListener('load', function() {
if (this.status != 200) {
console.error('loadRemoteBinaryFile: error ' + this.status + ' while trying to read url ' + url);
callback(null);
return;
}
let arrayBuffer = this.response;
let byteArray = new Uint8Array(arrayBuffer);
callback(byteArray);
});
req.open('get', url);
req.send();
}
// open local file selection dialog, and pass file as Uint8Array to callback
function loadLocalFile(callback) {
var input = document.createElement('input');
input.type = 'file';
input.addEventListener('change', function(e)
{
var file = event.target.files[0];
if (file.size > 1048576) {
alert('File is bigger than 1Mb');
selected_file = null;
return;
}
var reader = new FileReader();
reader.onload = function(f) {
if (this.error) {
console.error('loadRemoteBinaryFile: error ' + this.error + ' while trying to read file ' + file.name);
callback(null);
return;
}
let arrayBuffer = this.result;
let byteArray = new Uint8Array(arrayBuffer);
callback(file.name, byteArray);
};
reader.onabort = function() { alert('File read aborted'); }
reader.onerror = function() { alert('File read error'); }
reader.readAsArrayBuffer(file);
});
input.click();
}