-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfile.js
More file actions
97 lines (73 loc) · 2.31 KB
/
Copy pathfile.js
File metadata and controls
97 lines (73 loc) · 2.31 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const input = document.querySelector('input');
const upload = document.querySelector('#upload');
const i = document.querySelector('.img');
let binaryFileData = null;
let jsonFileData = null;
upload.addEventListener('click', () => {
console.log("uploading image....")
if(input.files.length > 1) {
handleMultipleFiles(input.files);
return
}
const file = input.files[0];
console.log(input.files)
const reader = new FileReader();
reader.readAsDataURL(file);
//display image
reader.onload = () => {
i.src = reader.result;
//copy to canvas
setTimeout(() => {
copyToCanvas();
}, 100);
}
})
function handleMultipleFiles(files){
//check if type is octet-stream
const file = files[0];
const file2 = files[1];
if(file.type === "application/octet-stream"){
//read file 1
const reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onload = () => {
//parse as base64
const data = new Uint8Array(reader.result);
console.log(reader.result)
console.log("binary file: ", data);
binaryFileData = data;
}
//read file 2
const reader2 = new FileReader();
reader2.readAsText(file2);
reader2.onload = () => {
const data = reader2.result;
// console.log("text file: ", data);
//convert to json
const json = JSON.parse(data);
console.log("json file: ", json);
jsonFileData = json;
}
}
//check if type is json
if(file.type === "application/json"){
//read file 1
const reader = new FileReader();
//read the jsno file
reader.readAsBinaryString(file);
reader.onload = () => {
const data = reader.result;
const json = JSON.parse(data);
console.log("json file: ", json);
jsonFileData = json;
}
//read file 2
const reader2 = new FileReader();
reader2.readAsArrayBuffer(file2);
reader2.onload = () => {
const data = new Uint8Array(reader2.result);
console.log("binary file: ", data);
binaryFileData = data;
}
}
}