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

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

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

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

請參考在網頁應用程式中使用本地檔案的更多細節與範例。

建構式

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

瀏覽器相容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic supportChrome 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.1Safari 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 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
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 Requires the vendor prefix: webkit
Edge No support NoFirefox 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 Requires 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.

 

實作備註

  • 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,