We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

翻譯不完整。請協助 翻譯此英文文件

File 介面提供了檔案的資訊並且允許網頁中的 JavaScript 存取檔案的內容。

File 物件通常是從使用者於 <input> 元素選擇之檔案所回傳的 FileList 物件當中取得,也可以來自拖放操作所產生的 DataTransfer 物件之中,或是由 HTMLCanvasElement 物件(元素物件)執行 mozGetAsFile() 方法後回傳。在 Gecko 引擎中,有專屬的程式碼能不需使用者操作即建立 File 物件來表示本地端的任一檔案(請參考 Implementation notes 以閱讀更多資訊)。

File 物件是一種特殊的 Blob,且可被用在任何接受 Blob 物件的地方。特別是 FileReaderURL.createObjectURL()createImageBitmap()XMLHttpRequest.send() 都能夠同樣接受 Blob 以及 File



回傳一個新建構的 File 物件。


File.lastModified Read only
回值檔案的最後修改時間,為 UNIX epoch 毫秒(自西元 1970 年一月 1 日零時至今)。
File.lastModifiedDate Read only
File 物件所代表之檔案的最後修改日期(Date)。
File.name Read only
File 物件所代表之檔案的名稱。
File.size Read only
File.webkitRelativePath Read only
Returns the path the URL of the File is relative to.
File.type Read only
回傳檔案的 MIME 類型。

File implements Blob, so it also has the following properties available to it:

File.size Read only
Returns the size of the file in bytes.
File.type Read only
Returns the MIME type of the file.


File 介面沒有定義任何方法,但繼承了 Blob 介面的方法:

Blob.slice([start[, end[, contentType]]])
Returns a new Blob object containing the data in the specified range of bytes of the source Blob.


Specification Status Comment
File API Working Draft Initial definition


FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support13 Yes


3 — 71

File() constructor13 No7 No11.510.1
lastModified13 Yes151016 No
lastModifiedDate1312151016 No
name13123.61016 Yes
type13 Yes3.61016 Yes
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support Yes Yes Yes


4 — 71

11.16 Yes
File() constructor Yes Yes Yes7 No6 Yes
lastModified Yes Yes Yes No No No Yes
lastModifiedDate Yes Yes Yes No No No Yes
name Yes Yes Yes No No No Yes
type Yes Yes Yes No No No Yes

1. Non-standard implementation.



  • 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 DOM File 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 create File objects from XPCOM component code instead of having to instantiate the nsIDOMFile object directly. The constructor takes, in contrast to Blob, 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 properties File.name, Blob.size, and methods on FileReader should be used instead.



此頁面的貢獻者: jackblackevo
最近更新: jackblackevo,