实现说明
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"));