File

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 .

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() ,和 XMLHttpRequest.send() accept both Blob s and File s.

Using files from web applications for more information and examples.

构造函数

File()
Returns a newly constructed File .

Instance properties

File.prototype.lastModified 只读
Returns the last modified time of the file, in millisecond since the UNIX epoch (January 1st, 1970 at Midnight).
File.prototype.lastModifiedDate 只读
Returns the last modified Date of the file referenced by the File 对象。
File.prototype.name 只读
Returns the name of the file referenced by the File 对象。
File.prototype.webkitRelativePath 只读
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.prototype.size 只读
Returns the size of the file in bytes.
File.prototype.type 只读
返回 MIME type of the file.

Instance methods

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

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

规范

规范 状态 Comment
File API
The definition of 'The File interface' in that specification.
工作草案 初始定义。

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
File Chrome 13 Edge 12 Firefox 7
7
不支持 3 — 7
Non-standard implementation.
IE 10 Opera 11.5 Safari 6 WebView Android ≤37 Chrome Android 18 Firefox Android 7
7
不支持 4 — 7
Non-standard implementation.
Opera Android 11.5 Safari iOS 6 Samsung Internet Android 1.0
File() 构造函数 Chrome 13 Edge 79 Firefox 7 IE No Opera 11.5 Safari 10.1 WebView Android ≤37 Chrome Android 18 Firefox Android 7 Opera Android 11.5 Safari iOS 6 Samsung Internet Android 1.0
lastModified Chrome 13 Edge 18 Firefox 15 IE No Opera 16 Safari 10 WebView Android Yes Chrome Android Yes Firefox Android No Opera Android No Safari iOS 10 Samsung Internet Android Yes
lastModifiedDate
弃用 非标
Chrome 13 Edge 12 Firefox 15 — 61 IE 10 Opera 16 Safari No WebView Android Yes Chrome Android Yes Firefox Android No Opera Android No Safari iOS No Samsung Internet Android Yes
name Chrome 13 Edge 12 Firefox 3.6 IE 10 Opera 16 Safari Yes WebView Android Yes Chrome Android Yes Firefox Android No Opera Android No Safari iOS No Samsung Internet Android Yes
type Chrome 13 Edge 12 Firefox 3.6 IE 10 Opera 16 Safari Yes WebView Android Yes Chrome Android Yes Firefox Android No Opera Android No Safari iOS Yes Samsung Internet Android Yes
webkitRelativePath Chrome 13
Prefixed
13
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge 13 Firefox 49 IE No Opera No Safari 11.1 WebView Android Yes Chrome Android 18
Prefixed
18
Prefixed
Prefixed Implemented with the vendor prefix: webkit
Firefox Android 49 Opera Android No Safari iOS 11.3 Samsung Internet Android 1.0
Prefixed
1.0
Prefixed
Prefixed Implemented with the vendor prefix: webkit

图例

完整支持
完整支持
不支持
不支持
非标。预期跨浏览器支持较差。
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
弃用。不要用于新网站。
见实现注意事项。
要求使用供应商前缀或不同名称。
要求使用供应商前缀或不同名称。

另请参阅