1. Overview
In the past, accessing local files was a headache for browser-based applications. Although JavaScript is playing an increasingly important role with the continuous development of Web 2.0 application technology, due to security considerations, JavaScript has always been unable to access local files. Therefore, in order to implement functions such as dragging and uploading local files in the browser, we have to resort to various technologies provided by specific browsers. For example, for IE, we need to use ActiveX controls to gain access to local files, and for Firefox, we also need to use plug-in development. Since the technical implementations of different browsers are different, in order for the program to support multiple browsers, our program will become very complex and difficult to maintain. But now, all this has been completely changed due to the emergence of File API.
File API is a draft submitted by Mozilla to W3C, aiming to launch a set of standard JavaScript APIs. Its basic function is to operate local files with JavaScript. For security reasons, this API only provides limited access to local files. With it, we can easily use pure JavaScript to read and upload local files. Currently, FireFox 3.6 is the first browser to support this feature. In addition, the latest versions of Google Chrome and Safari browsers also have corresponding support. The File API is expected to be part of the future HTML 5 specification currently being developed by the W3C.
2. File API Overview
The File API consists of a set of JavaScript objects and events. Gives developers the ability to manipulate files selected in the file selection control. Figure 1 shows the combination relationship of all JavaScript in the File API.
Type FileList contains a set of File objects. Usually FileList objects can be taken from the file field () in the form. A Blob object represents a set of raw binary streams that a browser can read. In the Blob object, the size attribute represents the size of the stream. The function slice() can split a long Blob object into small pieces. The File object inherits from the Blob object, and File-related properties are added to the Blob object. Among them, the attribute name represents the name of the file. This name removes the path information of the file and only retains the file name. The type attribute represents the MIME type of the file. The attribute urn represents the URN information of this file. To complete the file reading operation, a FileReader object instance is associated with a File or Blob object and provides three different file reading functions and 6 events.
The specific content of the file reading function:
readAsBinaryString() Read the file content, and the read result is a binary string. Each byte of the file will be represented as an integer in the range [0..255]. The function accepts a File object as parameter.
readAsText() Read the file content, and the read result is a string of text representing the file content. The function accepts a File object and the name of the text encoding as parameters.
readAsDataURL Read the file content, and the read result is a data: URL. DataURL is defined by RFC2397.
The specific content of the file reading event:
Event Name Event Description
Onloadstart Triggered when file reading starts.
Progress Triggered regularly when reading is in progress. The event parameters will contain the total amount of data read.
Abort Fired when the read is aborted.
Error Triggered when a reading error occurs.
Load Triggered when the read completes successfully.
Loadend Will be triggered when the read is completed, regardless of success or failure.
3. File API simple example
Next, we use a simple example to show the basic usage of File API. This example contains two code files, index.html contains the HTML code on the web side and JavaScript code for processing uploads; upload.jsp contains the code on the server side to receive uploaded files. Please see the sourcecode.zip in the attachment. In this example, we will display a traditional form with a File selection field. When the user selects a file and clicks submit, we use the File API to read the file content and upload the file to the server using Ajax through the XMLHttpRequest object. Figure 2 shows a screenshot of the demo in action.
We show the code step by step. Listing 1 shows the HTML portion of the code.
HTML portion of Listing 1 sample code
<body> <h1>File API Demo</h1> <p> <!-- 用于文件上传的表单元素 --> <form name="demoForm" id="demoForm" method="post" enctype="multipart/form-data" action="javascript: uploadAndSubmit();"> <p>Upload File: <input type="file" name="file" /></p> <p><input type="submit" value="Submit" /></p> </form> <div>Progessing (in Bytes): <span id="bytesRead"> </span> / <span id="bytesTotal"></span> </div> </p> </body>
As you can see, we use a normal