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

File 一般来说对象检索自 FileList 对象,返回作为用户选择文件的结果使用 <input> 元素,来自拖放操作的 DataTransfer 对象,或来自 mozGetAsFile() API 在 HTMLCanvasElement .

File 对象是特定种类的 Blob ,且可以用于 Blob 可以的任何上下文中。尤其, FileReader , URL.createObjectURL() , createImageBitmap() ,和 XMLHttpRequest.send() accept both Blob s and File

使用来自 Web 应用程序的文件 了解更多信息和范例。

  <div id="interfaceDiagram" style="display: inline-block; position: relative; width: 100%; padding-bottom: 11.666666666666666%; vertical-align: middle; overflow: hidden;"><svg style="display: inline-block; position: absolute; top: 0; left: 0;" viewbox="-50 0 600 70" preserveAspectRatio="xMinYMin meet"><a xlink:href="../API/Blob" target="_top"><rect x="1" y="1" width="75" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text  x="38.5" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">Blob</text></a><polyline points="76,25  86,20  86,30  76,25" stroke="#D4DDE4" fill="none"/><line x1="86" y1="25" x2="116" y2="25" stroke="#D4DDE4"/><a xlink:href="../API/File" target="_top"><rect x="116" y="1" width="75" height="50" fill="#F4F7F8" stroke="#D4DDE4" stroke-width="2px" /><text  x="153.5" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">File</text></a></svg></div>
					
  a:hover text { fill: #0095DD; pointer-events: all;}
					

构造函数

File()
返回新近构造 File .

实例特性

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 日期 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 实现 Blob , so it also has the following properties available to it:

File.prototype.size 只读

返回文件大小 (以字节为单位)。

File.prototype.type 只读
返回 MIME 类型为文件。

实例方法

File 接口未定义任何方法,但继承的方法来自 Blob 接口:

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

规范

规范 状态 注释
文件 API
定义为 ' File 接口' 在该规范中。
工作草案 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
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
名称 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

图例

完整支持

完整支持

不支持

不支持

非标。预期跨浏览器支持较差。

非标。预期跨浏览器支持较差。

弃用。不要用于新网站。

弃用。不要用于新网站。

见实现注意事项。

要求使用供应商前缀或不同名称。

要求使用供应商前缀或不同名称。

另请参阅

元数据

  • 最后修改:
  1. File
  2. 构造函数
    1. File()
  3. 特性
    1. fileName
    2. fileSize
    3. lastModified
    4. lastModifiedDate
    5. mozFullPath
    6. 名称
    7. type
    8. webkitRelativePath
  4. 方法
    1. getAsBinary()
    2. getAsDataURL()
    3. getAsText()
  5. 继承:
    1. Blob

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1