Blob: size 屬性

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

注意:此功能在 Web Workers 中可用。

sizeBlob 介面的一個只讀屬性,它返回 BlobFile 物件的大小,單位為位元組。

它表示 Blob(或基於 Blob 的物件,例如 File)中包含的資料位元組數。

示例

此示例使用型別為 file<input> 元素來要求使用者選擇一組檔案,然後遍歷這些檔案並輸出它們的名稱和位元組長度。

HTML

html
<input type="file" id="input" multiple />
<output id="output">Choose files…</output>

JavaScript

js
const input = document.getElementById("input");
const output = document.getElementById("output");

input.addEventListener("change", (event) => {
  output.innerText = "";

  for (const file of event.target.files) {
    output.innerText += `${file.name} has a size of ${file.size} bytes.\n`;
  }
});

結果

規範

規範
File API
# dfn-size

瀏覽器相容性

另見