由於本文件沒有此語言的翻譯版本,您閱讀的是英文版的內容。 幫助我們翻譯這篇文章!
File
介面提供了檔案的資訊並且允許網頁中的 JavaScript 存取檔案的內容。
File
物件通常是從使用者於 <input>
元素選擇之檔案所回傳的 FileList
物件當中取得,也可以來自拖放操作所產生的 DataTransfer
物件之中,或是由 HTMLCanvasElement
物件(元素物件)執行 mozGetAsFile()
方法後回傳。在 Gecko 引擎中,有專屬的程式碼能不需使用者操作即建立 File
物件來表示本地端的任一檔案(請參考 Implementation notes 以閱讀更多資訊)。
File
物件是一種特殊的 Blob
,且可被用在任何接受 Blob 物件的地方。特別是 FileReader
、URL.createObjectURL()
、createImageBitmap()
和 XMLHttpRequest.send()
都能夠同樣接受 Blob
以及 File
。
請參考在網頁應用程式中使用本地檔案的更多細節與範例。
<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="https://developer.mozilla.org/zh-TW/docs/Web/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="https://developer.mozilla.org/zh-TW/docs/Web/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.lastModified
Read only- 回值檔案的最後修改時間,為 UNIX epoch 毫秒(自西元 1970 年一月 1 日零時至今)。
File.lastModifiedDate
Read onlyFile
物件所代表之檔案的最後修改日期(Date
)。File.name
Read onlyFile
物件所代表之檔案的名稱。File.size
Read only- 回傳檔案大小。
File.webkitRelativePath
Read only- 回傳相對於
File
的網址位置。 File.type
Read only- 回傳檔案的 MIME 類型。
File
實作了 Blob
,因此它也有以下可用屬性:
方法
File
介面沒有定義任何方法,但繼承了 Blob
介面的方法:
Blob.slice([start[, end[, contentType]]])
-
回傳新的
Blob
物件,包含Blob
來源之指定位元組範圍的資料。
規範
規範 | 狀態 | 備註 |
---|---|---|
File API | Working Draft | 初次定義 |
瀏覽器相容性
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.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
File | Chrome Full support 13 | Edge Full support Yes | Firefox
Full support
7
| IE Full support 10 | Opera Full support 11.5 | Safari Full support 6 | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android
Full support
7
| Opera Android Full support 11.5 | Safari iOS Full support 6 | Samsung Internet Android Full support Yes |
File() constructor | Chrome Full support 13 | Edge No support No | Firefox Full support 7 | IE No support No | Opera Full support 11.5 | Safari Full support 10.1 | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support 7 | Opera Android No support No | Safari iOS Full support 6 | Samsung Internet Android Full support Yes |
lastModified | Chrome Full support 13 | Edge Full support 18 | Firefox Full support 15 | IE No support No | Opera Full support 16 | Safari No support No | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android No support No | Opera Android No support No | Safari iOS No support No | Samsung Internet Android Full support Yes |
lastModifiedDate | Chrome Full support 13 | Edge Full support 12 | Firefox No support 15 — 61 | IE Full support 10 | Opera Full support 16 | Safari No support No | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android No support No | Opera Android No support No | Safari iOS No support No | Samsung Internet Android Full support Yes |
name | Chrome Full support 13 | Edge Full support 12 | Firefox Full support 3.6 | IE Full support 10 | Opera Full support 16 | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android No support No | Opera Android No support No | Safari iOS No support No | Samsung Internet Android Full support Yes |
type | Chrome Full support 13 | Edge Full support Yes | Firefox Full support 3.6 | IE Full support 10 | Opera Full support 16 | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android No support No | Opera Android No support No | Safari iOS No support No | Samsung Internet Android Full support Yes |
webkitRelativePath | Chrome
Full support
13
| Edge Full support 13 | Firefox Full support 49 | IE No support No | Opera No support No | Safari No support No | WebView Android Full support Yes | Chrome Android
Full support
18
| Firefox Android Full support 49 | Opera Android No support No | Safari iOS No support No | Samsung 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.
實作備註
- In Gecko, you can use this API from within chrome code. See Using the DOM File API in chrome code for details. To use it from chrome code, JSM and Bootstrap scope, you have to import it using
Cu.importGlobalProperties(['File']);
- Starting from Gecko 6.0 (Firefox 6.0 / Thunderbird 6.0 / SeaMonkey 2.3), privileged code (such as extensions) can pass an
nsIFile
object to the DOMFile
constructor to specify the file to reference. - Starting from Gecko 8.0 (Firefox 8.0 / Thunderbird 8.0 / SeaMonkey 2.5), you can use
new File
to createFile
objects from XPCOM component code instead of having to instantiate thensIDOMFile
object directly. The constructor takes, in contrast toBlob
, as second argument the filename. The filename can be any String.new File( Array parts, String filename, BlobPropertyBag properties );
- The following non-standard properties and methods were removed in Gecko 7 (Firefox 7.0 / Thunderbird 7.0 / SeaMonkey 2.4):
File.fileName
,File.fileSize
,File.getAsBinary()
,File.getAsDataURL()
,File.getAsText(string encoding)
(bug 661876). Standard propertiesFile.name
,Blob.size
, and methods onFileReader
should be used instead.
參見
- 在網頁應用程式中使用本地檔案
FileReader
- Using the DOM File API in chrome code (for privileged code running in Gecko, such as Firefox add-ons)