HTML and CSS Reference
In-Depth Information
File Object
A File object represents a single file and provides information about it such as its name, size, and MIME
type. You also need a File object if you wish to read the contents of a file. Table 9-3 lists the properties of
the File object.
Table 9-3. Properties of the File Object
Name of a File along with its extension
Size of a file in bytes
MIME type of the file,
FileReader Object
A FileReader object allows you to read the contents of a File . The read operation is performed in
asynchronous fashion. This way, even very large files can be read without blocking other operations. The
FileReader object can read File contents as text, Base64, binary, or ArrayBuffer . Table 9-4 lists the
methods of the FileReader object that are responsible for reading a file.
Table 9-4. Methods of the Fi leReader Object
Property / Method
Reads a file as a text file
Reads a file as a data URL (Base64)
Reads a file as a raw binary string
Reads a file as an ArrayBuffer
C an be used to abort a file-reading operation
The readAsText() method is intended to be used with text-based files such as plain-text files, CSV
files, and XML files. The readAsDataURL() method encodes the file content in Base64 format and returns it
as a data URL. As described in more detail in Chapter 4, you know that the data URL format consists of
Base64-encoded data prefixed with the MIME type of the file, as shown in the following example:
The readAsDataURL() method also comes in handy in situations where binary data can't be transferred
to the server. One such situation is the jQuery $.ajax() method that sends text data to the server. The
readAsBinaryString() method reads files as raw binary data. The readAsArrayBuffer() method reads the
file contents as an ArrayBuffer ; an ArrayBuffer is a fixed-length binary data buffer.
The file-reading methods discussed here affect certain properties of a FileReader . These properties
can then be used to process the file contents or to flag an error to the end user. Table 9-5 lists these
Search WWH ::

Custom Search