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 Implementation notes for more information.)

A File object is a 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.

Constructor

File()
Returns a newly constructed File.

Properties

File.lastModified Read only
Returns the last modified time of the file, in millisecond since the UNIX epoch (January 1st, 1970 at Midnight).
File.lastModifiedDate Read only
Returns the last modified DateDate of the file referenced by the File object.
File.name Read only
Returns the name of the file referenced by the File object.
File.webkitRelativePath Read only
Returns the path the URL of the File is relative to.

File implements Blob, so it also has the following properties available to it:

File.size Read only
Returns the size of the file in bytes.
File.type Read only
Returns the MIME type of the file.

Methods

The File interface doesn't define any methods, but inherits methods from the Blob interface:

Blob.slice([start[, end[, contentType]]])
Returns a new Blob object containing the data in the specified range of bytes of the source Blob.

Specifications

Specification Status Comment
File API Working Draft Initial definition

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic supportChrome Full support 13Edge Full support YesFirefox Full support 7
Full support 7
No support 3 — 7
Notes
Notes Non-standard implementation.
IE Full support 10Opera Full support 11.5Safari Full support 6WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 7
Full support 7
No support 4 — 7
Notes
Notes Non-standard implementation.
Opera Android Full support 11.1Safari iOS Full support 6Samsung Internet Android Full support Yes
File() constructorChrome Full support 13Edge No support NoFirefox Full support 7IE No support NoOpera Full support 11.5Safari Full support 10.1WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 7Opera Android No support NoSafari iOS Full support 6Samsung Internet Android Full support Yes
lastModifiedChrome Full support 13Edge Full support 18Firefox Full support 15IE Full support 10Opera Full support 16Safari No support NoWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android Full support Yes
lastModifiedDate
DeprecatedNon-standard
Chrome Full support 13Edge Full support 12Firefox No support 15 — 61IE Full support 10Opera Full support 16Safari No support NoWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android Full support Yes
nameChrome Full support 13Edge Full support 12Firefox Full support 3.6IE Full support 10Opera Full support 16Safari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android Full support Yes
typeChrome Full support 13Edge Full support YesFirefox Full support 3.6IE Full support 10Opera Full support 16Safari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android Full support Yes
webkitRelativePathChrome Full support 13
Prefixed
Full support 13
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge No support NoFirefox Full support 49IE No support NoOpera No support NoSafari No support NoWebView Android Full support YesChrome Android Full support 18
Prefixed
Full support 18
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge Mobile No support NoFirefox Android Full support 49Opera Android No support NoSafari iOS No support NoSamsung Internet Android ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.
See implementation notes.
See implementation notes.
Requires a vendor prefix or different name for use.
Requires a vendor prefix or different name for use.

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 nsIFile object to the DOM File constructor to specify the file to reference.
  • Starting from Gecko 8.0 (Firefox 8.0 / Thunderbird 8.0 / SeaMonkey 2.5), you can use new File to create File objects from XPCOM component code instead of having to instantiate the nsIDOMFile object directly. The constructor takes, in contrast to Blob, as second argument the filename. The filename can be any String.
    new 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 properties File.name, Blob.size, and methods on FileReader should be used instead.

See also