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
只读
日期
of the file referenced by the
File
对象。
File.prototype.name
只读
File
对象。
File.prototype.webkitRelativePath
只读
File
is relative to.
File
实现
Blob
, so it also has the following properties available to it:
File.prototype.size
只读
返回文件大小 (以字节为单位)。
File.prototype.type
只读
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()
File
成
ReadableStream
that can be used to read the
File
contents.
Blob.prototype.text()
File
into a stream and reads it to completion. It returns a promise that resolves with a
USVString
(text).
Blob.prototype.arrayBuffer()
File
into a stream and reads it to completion. It returns a promise that resolves with an
ArrayBuffer
.
| 规范 | 状态 | 注释 |
|---|---|---|
|
文件 API
定义为 '
File
接口' 在该规范中。
|
工作草案 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
File
|
Chrome 13 | Edge 12 |
Firefox
7
|
IE 10 | Opera 11.5 | Safari 6 | WebView Android ≤37 | Chrome Android 18 |
Firefox Android
7
|
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
|
Edge 13 | Firefox 49 | IE No | Opera No | Safari 11.1 | WebView Android Yes |
Chrome Android
18
Prefixed
|
Firefox Android 49 | Opera Android No | Safari iOS 11.3 |
Samsung Internet Android
1.0
Prefixed
|
完整支持
不支持
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
见实现注意事项。
要求使用供应商前缀或不同名称。
FileReader