The File interface provides information about files and allows to access their content.
These are generally retrieved from a FileList object returned as a result of a user selecting files using the <input> element, from a drag and drop operation's DataTransfer object, or from the mozGetAsFile() API on an HTMLCanvasElement. In Gecko, from privileged code it is possible to call the constructor directly with a string path or an nsIFile. See Using the DOM File API in chrome code for details.
The file reference can be saved when the form is submitted while the user is offline, so that the data can be retrieved and uploaded when the Internet connection is restored.
Properties
The File interface also inherits properties from the Blob interface.
File.lastModifiedDateRead only- The last modified
Dateof the file referenced by theFileobject. File.nameRead only- The name of the file referenced by the
Fileobject. File.fileNameRead only Obsolete since Gecko 7.0- The name of the file referenced by the
Fileobject. File.fileSizeRead only Obsolete since Gecko 7.0- The size of the referenced file in bytes.
Methods
The File interface also inherits methods from the Blob interface.
File.getAsBinary()Obsolete since Gecko 7.0- Returns a string containing the file's data in raw binary format.
File.getAsDataURL()Obsolete since Gecko 7.0- A string containing the file's data encoded as a
data:URL. File.getAsText(string encoding)Obsolete since Gecko 7.0- Returns the file's contents as a string in which the file's data is interpreted as text using a given encoding.
Specifications
| Specification | Status | Comment |
|---|---|---|
| File API | Working Draft | Initial definition. |
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|
| Basic support | 13 | 3.0 (1.9) (non standard) 7 (7) (standard) |
10.0 | 11.5 | 6.0 |
| Feature | Android | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|
| basic support | Not supported | 25 | Not supported | 11.1 | 6.0 |
Implementation notes
Gecko notes
- In Gecko, you can use this API from chrome code. See Using the DOM File API in chrome code for details.
- Starting in Gecko 6.0 (Firefox 6.0 / Thunderbird 6.0 / SeaMonkey 2.3), privileged code (such as extensions) can pass an
nsIFileobject to the DOMFileconstructor to specify the file to reference. - Starting in Gecko 8.0 (Firefox 8.0 / Thunderbird 8.0 / SeaMonkey 2.5), you can use
new Fileto createFileobjects from XPCOM component code instead of having to instantiate thensIDOMFileobject directly. The constructor takes, in contrast to Blob, as second argument the filename. The filename can be any String.File File( Array parts, String filename, BlobPropertyBag properties );
Chrome Code - Scope Availability
To use from chrome code, JSM and Bootstrap scope, you have to import it like this:
Cu.importGlobalProperties(['File']);
URL is available in Worker scopes.