-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFile_FileReader.js
More file actions
83 lines (50 loc) · 2.6 KB
/
Copy pathFile_FileReader.js
File metadata and controls
83 lines (50 loc) · 2.6 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
//: File and FileReader
/* A File object inherits from blob and extended with filesystem-related capabilities */
//? new File(fileParts, fileName, [option])
/* => fileParts - is an array of Blob/BufferSource/String values.
=> fileName - file name string
=> options - optional object
=> lastModified - the timestamp (integer data) of last modifiaction.*/
//* 2 second, more often we get a file from <input type="file> or drag'n' drop or other browser interface."
<input type="file" onChange="showFile(this)" />;
function showFile(input) {
let file = input.files[0];
console.log(`File name: ${file.name}`); // my.png
console.log(`last modified: ${file.lastModified}`); // e.g.234375475747
}
/* The input may select multiple files, so input.files is an array-like object with them. Here we have only one file, so we just take input.files[0] */
//* FideReader
/* FileReader is an object with the sole purpose of reading data from Blob (and hence File too) objects. */
let reader = new FileReader(); // no arguments
// The main methods:
/*
=> readAsArrayBuffer(blob) - read the data in binary format "ArrayBuffer".
=> readAsText(blob, [encoding]) - read the data as a text string with the given encoding (utf-8 by default).
=> readAsDataURL(blob) - read the binary data encode it as base64 data url.
=> abort() - cancel the operation.
? The choise of read* method depends on which format we prefer, how going to use the data.
=> readAsArrayBuffer – for binary files, to do low-level binary operations. For high-level operations, like slicing, File inherits from Blob, so we can call them directly, without reading.
=> readAsText – for text files, when we’d like to get a string.
=> readAsDataURL – when we’d like to use this data in src for img or another tag. There’s an alternative to reading a file for that, as discussed in chapter Blob: URL.createObjectURL(file).
? As the reading proceeds, there are events:
=> loadstart - loading started
=> progress - occurs during reading
=> load - no error, reading complete
=> abort - abort() called
=> error - error has occurred
=> loadend - reading finished with either success or failure
? When the reading is finished, we can access the result as:
=> reader.result is the result (if successful)
=> reader.error is the error (if failed)
*/
{
/* <input type"file" onchange="readFile(this)"> */
}
function readFile(input) {
let file = input.files[0];
let reader = new FileReader();
reader.readAsText(file);
reader.onload(() => console.log(readder.result));
reader.onerror(() => console.log(reader.error));
}
//? FileReaderSyn is available inside Web workers