The File interface provides information about files and allows JavaScript in a web page to access their content.
File objects 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, privileged code can create File objects representing any local file without user interaction (see Gecko notes for more information.)
A File object is specific kind of a Blob, and can be used in any context that a Blob can. In particular, FileReader, URL.createObjectURL(), createImageBitmap(), and XMLHttpRequest.send() accept both Blobs and Files.
See Using files from web applications for more information and examples.
Properties
File.lastModifiedDateRead only- The last modified
Dateof the file referenced by theFileobject. File.nameRead only- The name of the file referenced by the
Fileobject.
The File interface also inherits properties from the Blob interface:
Blob.isClosedRead only- A boolean value, indicating whether the
Blob.close()method has been called on the blob. Closed blobs can not be read. Blob.sizeRead only- The size, in bytes, of the data contained in the
Blobobject. Blob.typeRead only- A string indicating the MIME type of the data contained in the
Blob. If the type is unknown, this string is empty.
Methods
The File interface doesn't define any methods, but inherits methods from the Blob interface:
Blob.close()- Closes the blob object, possibly freeing underlying resources.
Blob.slice([start[, end[, contentType]]])- Returns a new
Blobobject containing the data in the specified range of bytes of the sourceBlob.
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)[1] 7 (7) |
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 |
[1] Non-standard implementation.
Implementation notes
- In Gecko, you can use this API from within chrome code. See Using the DOM File API in chrome code for details. To use it from chrome code, JSM and Bootstrap scope, you have to import it using
Cu.importGlobalProperties(['File']); - Starting from 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 from 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 toBlob, as second argument the filename. The filename can be any String.File File( Array parts, String filename, BlobPropertyBag properties );
- The following non-standard properties and methods were removed in Gecko 7 (Firefox 7.0 / Thunderbird 7.0 / SeaMonkey 2.4):
File.fileName,File.fileSize,File.getAsBinary(),File.getAsDataURL(),File.getAsText(string encoding)(bug 661876). Standard propertiesFile.name,Blob.size, and methods onFileReadershould be used instead.
See also
- Using files from web applications
FileReader- Using the DOM File API in chrome code (for privileged code running in Gecko, such as Firefox add-ons)