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 {{domxref("FileList")}} object returned as a result of a user selecting files using the {{HTMLElement("input")}} element, from a drag and drop operation's {{domxref("DataTransfer")}} object, or from the mozGetAsFile()
API on an {{domxref("HTMLCanvasElement")}}. In Gecko, privileged code can create File
objects representing any local file without user interaction (see {{anch("Gecko notes")}} for more information.)
A File
object is a specific kind of a {{domxref("Blob")}}, and can be used in any context that a Blob can. In particular, {{domxref("FileReader")}}, {{domxref("URL.createObjectURL()")}}, {{domxref("ImageBitmapFactories.createImageBitmap()", "createImageBitmap()")}}, and {{domxref("XMLHttpRequest", "", "send()")}} accept both Blob
s and File
s.
See Using files from web applications for more information and examples.
Properties
The File
interface also inherits properties from the {{domxref("Blob")}} interface:
- {{domxref("File.lastModified")}} {{readonlyinline}}
- Returns the last modified time of the file, in millisecond since the UNIX epoch (January 1st, 1970 at Midnight).
- {{domxref("File.lastModifiedDate")}} {{readonlyinline}} {{deprecated_inline}} {{gecko_minversion_inline("15.0")}}
- Returns the last modified
Date
of the file referenced by theFile
object. - {{domxref("File.name")}} {{readonlyinline}}
- Returns the name of the file referenced by the
File
object. - {{domxref("File.webkitRelativePath")}} {{readonlyinline}} {{non-standard_inline}}
- Returns the path the URL of the {{domxref("File")}} is relative to.
Methods
The File
interface doesn't define any methods, but inherits methods from the {{domxref("Blob")}} interface:
Specifications
Specification | Status | Comment |
---|---|---|
{{SpecName('File API')}} | {{Spec2('File API')}} | Initial definition |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 13 | {{CompatGeckoDesktop("1.9")}}[1] {{CompatGeckoDesktop("7")}} |
10.0 | 11.5 | 6.0 |
name |
{{CompatUnknown}} | {{CompatGeckoDesktop("1.9.2")}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
lastModifiedData {{deprecated_inline}} |
{{CompatUnknown}} | {{CompatGeckoDesktop("15")}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
webkitRelativePath {{non-standard_inline}} |
{{CompatVersionUnknown}} | {{CompatGeckoDesktop("49")}} | {{CompatUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
Feature | Android | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | {{CompatNo}} | {{CompatGeckoMobile("1.9")}}[1] {{CompatGeckoMobile("7")}} |
{{CompatNo}} | 11.1 | 6.0 |
name |
{{CompatUnknown}} | {{CompatGeckoMobile("1.9.2")}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
lastModifiedData {{deprecated_inline}} |
{{CompatUnknown}} | {{CompatGeckoMobile("15")}} | {{CompatUnknown}} | {{CompatUnknown}} | {{CompatUnknown}} |
webkitRelativePath {{non-standard_inline}} |
{{CompatVersionUnknown}} | {{CompatGeckoMobile("49")}} | {{CompatUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
[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 {{geckoRelease("6.0")}}, privileged code (such as extensions) can pass an {{interface("nsIFile")}} object to the DOM
File
constructor to specify the file to reference. - Starting from Gecko 8.0 {{geckoRelease("8.0")}}, you can use
new File
to createFile
objects from XPCOM component code instead of having to instantiate the {{interface("nsIDOMFile")}} object directly. The constructor takes, in contrast to {{domxref("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 {{geckoRelease("7.0")}}: {{domxref("File.fileName")}}, {{domxref("File.fileSize")}}, {{domxref("File.getAsBinary()")}}, {{domxref("File.getAsDataURL()")}}, {{domxref("File.getAsText()","File.getAsText(string encoding)")}} ({{bug("661876")}}). Standard properties {{domxref("File.name")}}, {{domxref("Blob.size")}}, and methods on {{domxref("FileReader")}} should be used instead.
See also
- Using files from web applications
- {{domxref("FileReader")}}
- Using the DOM File API in chrome code (for privileged code running in Gecko, such as Firefox add-ons)