FormData.entries Enumerates Posted Fields

FormData.entries yields every field and file as insertion-ordered pairs, while has/get/delete probe individual entries before a POST goes out.

JavascriptFile Upload

Code

javascriptsnippet
15 lines
// 构造与浏览器 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"));

Output

Captured from a sandboxed run of this snippet in the build pipeline.

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

Inside the snippet

Repetition is confined to for (const [key, value] of form.entries()) — the rest of the file is straight-line.

Module-level bindings hold the data — form — with no function scope in between.

Using it

Running the file prints labeled results such as has threshold:, has threshold after delete: — each operation’s effect is visible in the output.

Key statements

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

FAQ

Does this work in a bare project with no dependencies file?
Yes — a bare project runs it as-is, since nothing needs declaring. Its state lives in module-level bindings: `form`.