文件(File)接口提供有关文件的信息,并允许网页中的 JavaScript 访问其内容。

通常情况下, File 对象是来自用户在一个 <input> 元素上选择文件后返回的 FileList 对象,也可以是来自由拖放操作生成的 DataTransfer 对象,或者来自 HTMLCanvasElement 上的 mozGetAsFile() API。在Gecko中,特权代码可以创建代表任何本地文件的File对象,而无需用户交互(有关详细信息,请参阅注意事项

File 对象是特殊类型的 Blob,且可以用在任意的 Blob 类型的 context 中。比如说, FileReader, URL.createObjectURL(), createImageBitmap(), 及 XMLHttpRequest.send() 都能处理 Blob File

参考 从Web应用程序使用文件 了解更多信息和例子。

构造函数

File()
返回一个新构建的文件对象(File)。

属性

File 接口也继承了 Blob 接口的属性:

File.lastModified 只读
返回当前 File 对象所引用文件最后修改时间,自 UNIX 时间起始值(1970年1月1日 00:00:00 UTC)以来的毫秒数。
File.lastModifiedDate 只读
返回当前 File 对象所引用文件最后修改时间的 Date 对象。
File.name 只读
返回当前 File 对象所引用文件的名字。
File.size 只读
返回文件的大小。
File.webkitRelativePath 只读
返回 File 相关的 path 或 URL。
File.type 只读
返回文件的 多用途互联网邮件扩展类型(MIME Type)

方法

File 接口没有定义任何方法,但是它从 Blob 接口继承了以下方法:

Blob.slice([start[, end[, contentType]]])
返回一个新的 Blob 对象,它包含有源 Blob 对象中指定范围内的数据。

规范

规范 状态 注释
File API Working Draft Initial definition

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
FileChrome 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.5Safari 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 No support NoOpera Full support 16Safari No support NoWebView Android Full support YesChrome Android Full support YesEdge Mobile No support NoFirefox 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 Full support 13Firefox 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.

注意事项

  • 在 Gecko 中,你可以从 Chrome 代码中使用这个 API。详细内容参见 Using the DOM File API in chrome code。若要从 chrome 代码, JSM 和引导范围中使用它,你必须使用 Cu.importGlobalProperties(['File']); 来导入它。
  • 从Gecko 6.0 (Firefox 6.0 / Thunderbird 6.0 / SeaMonkey 2.3) 开始,在特权代码(比如扩展中的代码)中,可以将一个 nsIFile 对象传入 File 构造函数,从而生成一个 File 对象。
  • 从Gecko 8.0 (Firefox 8.0 / Thunderbird 8.0 / SeaMonkey 2.5) 开始,在 XPCOM 组件代码中,你可以直接使用 new File 来创建一个 File 对象,而不需要像以前那样必须实例化一个 nsIDOMFile 对象。File 对象和 Blob 相反,使用第二个参数作为文件名。文件名可以是任意的字符串。
    new File( Array parts, String filename, BlobPropertyBag properties);
    
  • 下列非标准属性及方法在 Gecko 7 (Firefox 7.0 / Thunderbird 7.0 / SeaMonkey 2.4) 中就被移除了: File.fileNameFile.fileSizeFile.getAsBinary()File.getAsDataURL()File.getAsText(string encoding) (bug 661876)。应当使用 File.nameBlob.size,和 FileReader 的方法来代替。

参见

文档标签和贡献者

最后编辑者: mdnwebdocs-bot,