A File object inherits from Blob and is extended with filesystem-related capabilities.
There are two ways to obtain it.
First, there’s a constructor, similar to Blob:
new File(fileParts, fileName, [options])
fileParts– is an array of Blob/BufferSource/String value, same asBlob.fileName– file name string.options– optional object:lastModified– a timestamp (integer date) of last modification.
Second, more often we get a file from <input type="file"> or drag’n’drop or other browser interfaces. Then the file gets these from OS.
For instance:
<input type="file" onchange="showFile(this)">
<script>
function showFile(input) {
let file = input.files[0];
alert(`File name: ${file.name}`); // e.g my.png
alert(`Last modified: ${file.lastModified}`); // e.g 1552830408824
}
</script>
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].
FileReader
FileReader is an object with the sole purpose of reading data from Blob (and hence File too) objects.
It delivers the data using events, as reading from disk may take time.
The constructor:
let reader = new FileReader(); // no arguments
The main methods:
readAsArrayBuffer(blob)– read the data asArrayBuffer