This is the 13th day of my participation in the August More Text Challenge
What is the File
-
The File interface provides information about a File and allows JavaScript in a web page to access its contents.
-
In general, A File object is a FileList object returned by the user after selecting a File on a element (the files property of the element). It can also be a File from a DataTransfer object generated by a drag-and-drop operation. Or from the mozGetAsFile() API on HTMLCanvasElement.
-
File.lastModified: returns the number of milliseconds since the start of UNIX time (00:00:00 UTC, January 1, 1970) when the File referenced by the current File object was lastModified. Read-only \
-
File.lastModifiedDate: A Date object that returns the last modified time of the File referenced by the current File object. Read-only \
-
File.name: Returns the name of the File referenced by the current File object. Read-only \
-
File.size: Returns the size of the File. Read-only \
-
File.webkitRelativePath: Returns the path or URL associated with File. Read-only \
-
File.type: The multipurpose Internet Mail extension type (MIME Type) of the returned File. Read-only \
There are two ways to get files
The first:
Constructor creation
new File(fileParts, fileName, [options])
Copy the code
- FileParts — an array of Blob/BufferSource/String values.
- FileName – Indicates the name of the file.
- Options — Optional objects:
- LastModified – Timestamp of the last modification (integer date).
The second:
Or drag and drop or other browser interface to get the file. In this case, file will get this information from the operating system (OS). Since File is inherited from Blob, the File object has the same properties, plus:
- Name — file name,
- LastModified – Timestamp of the last modification.
Example:
<input type="file" onchange="consoleFile(this)"> <script> function consoleFile(input) { let file = input.files[0]; alert(`File name: ${file.name}`); PNG alert(' Last Modified: ${file.lastModified} '); // for example 2020.20.20} </script>Copy the code
TIP: Input (input) can select multiple files, so input.files is an array-like object. Here we only have one file, so we just take input.files[0].
What is the FileReader
A FileReader is an object whose sole purpose is to read data from a Blob (and therefore from a File) object. Rather, a constructor that reads Blob objects to help us manipulate binaries.
let reader = new FileReader(); // No argumentsCopy the code
Main usage methods:
- ReadAsArrayBuffer (BLOB) — An ArrayBuffer that reads data in binary format.
- ReadAsText (blob, [encoding]) — reads the data as a text string in the given encoding (default utF-8 encoding).
- ReadAsDataURL (BLOB) — Reads binary data and encodes it as base64’s data URL.
- Abort () — Cancels the operation.
These methods have the following six declaration cycles to operate
- Loadstart — Starts loading.
- Progress – appears during the read process.
- Load — Reads complete without error.
- Abort — Abort () is called.
- Error — An error occurs.
- Loadend – The read completes, whether it succeeds or fails.
After the read is complete, we can obtain the read results in the following two ways
- Reader.result is the result (if successful)
- Reader. Error is error (if failed).
Example:
<input type="file" onchange="readFile(this)">
<script>
function readFile(input) {
let file = input.files[0];
let reader = new FileReader();
reader.readAsText(file);
reader.onload = function() {
console.log(reader.result);
};
reader.onerror = function() {
console.log(reader.error);
};
}
</script>
Copy the code
Use FileReader to manipulate blobs
As we mentioned in the Blob chapter, FileReader reads not only files, but also any Blob. We can use it to convert bloBs to other formats:
- ReadAsArrayBuffer (BLOB) — Convert to ArrayBuffer,
- ReadAsText (blob, [encoding]) — Convert to a string (an alternative to TextDecoder),
- ReadAsDataURL (BLOB) — DataURL converted to Base64.