← 中文资源

简体中文版本

FormData.entries 枚举待发送字段

FormData.entries 按插入顺序产出每个字段与文件条目,文件仍是 Blob;has/get/delete 可在发出 POST 前逐条探测与删除。

阅读英文原文

运行结果

以下输出由构建管线在沙箱中真实运行该示例捕获。

service => api
threshold_ms => 150
snapshot => file application/json
has threshold: true
has threshold after delete: false

实现说明

for (const [key, value] of form.entries()) 承担全部的逐项处理。 顶层就这几处状态:form。

使用要点

  • 跑完文件,输出里会留下标记——has threshold:、has threshold after delete:。

常见问题

在一个没有任何依赖声明的空项目里能跑吗?

可以——空项目原样能跑,因为什么都不用声明。 跨调用保留的状态放在这些顶层绑定里:form。

核心语句

form.set("snapshot", new Blob([JSON.stringify({ p95: 1420 })], { type: "application/json" }), "snapshot.json");

代码示例

// 构造与浏览器 POST 相同的形态:字段 + 文件条目
const form = new FormData();
form.set("service", "api");
form.set("threshold_ms", "150");
form.set("snapshot", new Blob([JSON.stringify({ p95: 1420 })], { type: "application/json" }), "snapshot.json");

// entries 按插入顺序枚举:普通字段与文件同为 [key, value] 对
for (const [key, value] of form.entries()) {
  console.log(key, "=>", value instanceof Blob ? `file ${value.type}` : value);
}

// has/get 快速探测:delete 后 has 变 false
console.log("has threshold:", form.has("threshold_ms"));
form.delete("threshold_ms");
console.log("has threshold after delete:", form.has("threshold_ms"));