--- title: File slug: Web/API/File tags: - API - File API - Interface - NeedsTranslation - Reference - TopicStub - Web translation_of: Web/API/File ---
{{APIRef}}

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")}}.

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 Blobs and Files.

See Using files from web applications for more information and examples.

{{InheritanceDiagram}}

Constructor

{{DOMxRef("File.File", "File()")}}
Returns a newly constructed File.

Instance properties

{{DOMxRef("File.prototype.lastModified")}} {{ReadOnlyInline}}
Returns the last modified time of the file, in millisecond since the UNIX epoch (January 1st, 1970 at Midnight).
{{DOMxRef("File.prototype.lastModifiedDate")}} {{Deprecated_Inline}} {{ReadOnlyInline}}
Returns the last modified {{JSxRef("Date")}} of the file referenced by the File object.
{{DOMxRef("File.prototype.name")}}{{ReadOnlyInline}}
Returns the name of the file referenced by the File object.
{{DOMxRef("File.prototype.webkitRelativePath")}} {{Non-standard_Inline}} {{ReadOnlyInline}}
Returns the path the URL of the {{DOMxRef("File")}} is relative to.

File implements {{DOMxRef("Blob")}}, so it also has the following properties available to it:

{{DOMxRef("File.prototype.size")}} {{ReadOnlyInline}}
Returns the size of the file in bytes.
{{DOMxRef("File.prototype.type")}} {{ReadOnlyInline}}
Returns the MIME type of the file.

Instance methods

The File interface doesn't define any methods, but inherits methods from the {{DOMxRef("Blob")}} interface:

{{DOMxRef("Blob.prototype.slice()", "Blob.prototype.slice([start[, end[, contentType]]])")}}
Returns a new Blob object containing the data in the specified range of bytes of the source Blob.
{{DOMxRef("Blob.prototype.stream()")}}
Transforms the File into a {{DOMxRef("ReadableStream")}} that can be used to read the File contents.
{{DOMxRef("Blob.prototype.text()")}}
Transforms the File into a stream and reads it to completion. It returns a promise that resolves with a {{DOMxRef("USVString")}} (text).
{{DOMxRef("Blob.prototype.arrayBuffer()")}}
Transforms the File into a stream and reads it to completion. It returns a promise that resolves with an {{DOMxRef("ArrayBuffer")}}.

Specifications

Specification Status Comment
{{SpecName('File API', "#file-section", "The File interface")}} {{Spec2('File API')}} Initial definition.

Browser compatibility

{{Compat("api.File")}}

See also