File

Introduced in Gecko 1.9

(Firefox 3)

The File object provides information about -- and access to the contents of -- files. These are generally retrieved from a FileList object returned as a result of a user selecting files using the input element, or from a drag and drop operation's DataTransfer object.

Note: In Gecko, you can use this API from chrome code. See Using the DOM File API in chrome code for details.

Gecko 2.0 note
(Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1)

Starting in Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1) , the File object inherits from the Blob  interface, which provides methods and properties that give further information about the file.

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.

Note: The File object as implemented by Gecko offers several non-standard methods for reading the contents of the file. These should not be used, as they will prevent your web application from being used in other browsers, as well as in future versions of Gecko, which will likely remove these methods.

Method overview

DOMString getAsBinary(); Non-standard Obsolete since Gecko 7.0
DOMString getAsDataURL(); Non-standard Obsolete since Gecko 7.0
DOMString getAsText(in DOMString encoding); Non-standard Obsolete since Gecko 7.0

Properties

Property Type Description
fileName DOMString The name of the file referenced by the File object. Read only. Obsolete since Gecko 7.0
fileSize unsigned long long The size of the referenced file in bytes. Read only. Obsolete since Gecko 7.0
mozFullPath DOMString The full path of the referenced file; available only to code with UniversalFileRead privileges in chrome. Read only. Non-standard Requires Gecko 1.9.2
mozFullPathInternal DOMString This is an internal-use-only property that does not do security checks. It can only be used from native code, and is used to optimize performance in special cases in which security is not a concern. Read only. Native code only! Non-standard Requires Gecko 2.0
name DOMString The name of the file referenced by the File object. Read only. Requires Gecko 1.9.2
size unsigned long long The size (in bytes) of the file referenced by the File object. Read only. Requires Gecko 1.9.2
type DOMString The type (MIME type) of the file referenced by the File object. Read only. Requires Gecko 1.9.2

Methods

These methods are all non-standard. To read the contents of a file, you should use the FileReader object. See Using files from web applications for examples.

Obsolete since Gecko 7.0 (Firefox 7.0 / Thunderbird 7.0 / SeaMonkey 2.4)

getAsBinary()

Returns a string containing the file's data in raw binary format.

Note: This method is obsolete; you should use the FileReader method readAsBinaryString() or readAsArrayBuffer() instead.
DOMString getAsBinary();
Parameters

None.

Return value

A string containing the raw binary contents of the file.

Obsolete since Gecko 7.0 (Firefox 7.0 / Thunderbird 7.0 / SeaMonkey 2.4)

getAsDataURL()

Returns a string containing a data: URL that encodes the entire contents of the referenced file.

Note: This method is obsolete; you should use the FileReader method readAsDataURL() instead.
DOMString getAsDataURL();
Parameters

None.

Return value

A string containing the file's data encoded as a data: URL.

Obsolete since Gecko 7.0 (Firefox 7.0 / Thunderbird 7.0 / SeaMonkey 2.4)

getAsText()

Returns the file's contents as a string in which the file's data is interpreted as text using a given encoding.

Note: This method is obsolete; you should use the FileReader method readAsText() instead.
DOMString getAsText(
  in DOMString encoding
 );
Parameters
encoding
A string indicating the encoding to use for the returned data. If this string is empty, UTF-8 is assumed.
Return value

A string containing the file's data interpreted as text in the specified encoding.

Notes

Gecko notes

Gecko 6.0 note
(Firefox 6.0 / Thunderbird 6.0 / SeaMonkey 2.3)

Starting in 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.

Gecko 8.0 note
(Firefox 8.0 / Thunderbird 8.0 / SeaMonkey 2.5)

Starting in 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.

Specification

Tags (3)

Edit tags

Attachments (0)

 

Attach file