Blob:size 屬性

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

備註: 此功能可在 Web Worker 中使用。

Blob 介面的 size 唯讀屬性回傳 BlobFile 的大小(以位元組為單位)。

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

範例

此範例使用一個類型為 file<input> 元素,讓使用者選擇一組檔案,然後遍歷這些檔案並輸出它們的名稱及大小(以位元組為單位)。

HTML

html
<input type="file" id="input" multiple /> <output id="output">選擇檔案…</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} 的大小為 ${file.size} 位元組。\n`;
  }
});

結果

規範

Specification
File API
# dfn-size

瀏覽器相容性

參見