Encode FormData
content into the multipart/form-data
format
- browsers or Deno
import { FormDataEncoder } from "https://code4fukui.github.io/form-data-encoder/FormDataEncoder.js";
const form = new FormData();
form.set("a", 15);
form.set("b", "test");
form.set("file", new Blob([new Uint8Array([1, 2, 3])]), "a.txt");
const { headers, body } = await FormDataEncoder.encode(form);
console.log(headers, new TextDecoder().decode(body));
TypeScript → JavaScript
npm i
npm run build
- {FormDataLike} form - FormData object to encode. This object must be a spec-compatible FormData implementation.
- {string} [boundary] - An optional boundary string that will be used by the encoder. If there's no boundary string is present, FormDataEncoder will generate it automatically.
- {object} [options] - FormDataEncoder options.
- {boolean} [options.enableAdditionalHeaders = false] - When enabled, the encoder will emit additional per part headers, such as
Content-Length
. Please note that the web clients do not include these, so when enabled this option might cause an error ifmultipart/form-data
does not consider additional headers.
Creates a multipart/form-data
encoder.
Returns boundary string.
Returns Content-Type header.
Return Content-Length header.
Returns headers object with Content-Type and Content-Length header.
Creates an iterator allowing to go through form-data parts (with metadata). This method will not read the files.
Creates an async iterator allowing to perform the encoding by portions. This method will also read files.
An alias for Encoder#values()
method.
An alias for Encoder#encode()
method.
Check if a value is File-ish object.
- {unknown} value - a value to test
Check if a value is FormData-ish object.
- {unknown} value - a value to test