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 January 2020.

备注: 此特性在 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

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
size

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Has more compatibility info.

参见