Skip to content

Commit 0214ae7

Browse files
authored
feat: FormData support and Request.formData and Response.formData (#1144)
1 parent 42d429b commit 0214ae7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1059
-206
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
# Blob()
8+
9+
The **`Blob()`** constructor creates a `Blob` object, which represents a file-like object of immutable, raw data.
10+
11+
## Syntax
12+
13+
```js
14+
new Blob()
15+
new Blob(array)
16+
new Blob(array, options)
17+
```
18+
19+
> **Note:** `Blob()` can only be constructed with `new`. Attempting to call it without `new` throws a `TypeError`.
20+
21+
### Parameters
22+
23+
- `array` _**optional**_
24+
25+
- : An array of values to include in the `Blob`. These can be [`ArrayBuffer`](../../globals/ArrayBuffer/ArrayBuffer.mdx), [`ArrayBufferView`](../../globals/ArrayBufferView/ArrayBufferView.mdx), [`Blob`](../../globals/Blob/Blob.mdx), or strings. If any of these elements is a [`Blob`](../../globals/Blob/Blob.mdx), its content (and not the object itself) is copied into the Blob being constructed.
26+
27+
- `options` _**optional**_
28+
29+
- : An object containing optional attributes for the `Blob`.
30+
- `type`
31+
- : A string indicating the MIME type of the data. The default value is the empty string `""`.
32+
- `endings`
33+
- : A string indicating how to handle line endings in the data. This can be either `"transparent"` (default) to keep line endings unchanged, or `"native"` to convert line endings to the platform's native line endings (e.g., `\r\n` on Windows).
34+
35+
### Return value
36+
37+
A new `Blob` object containing the specified data.
38+
39+
## Description
40+
41+
`Blob` objects represent data that isn't necessarily in a JavaScript-native format. The `File` interface is based on `Blob`, inheriting its functionality and expanding it to support files on the user's system.
42+
43+
To construct a `Blob` from other non-blob objects and data, use the `Blob()` constructor. To create a blob that contains a subset of another blob's data, use the [`slice()`](../../globals/Blob/prototype/slice.mdx) method.
44+
45+
The `type` property of a `Blob` object will match the MIME type specified in the constructor's `options` parameter, defaulting to an empty string if not specified.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
# Blob.arrayBuffer()
8+
9+
The **`arrayBuffer()`** method of the `Blob` interface returns a `Promise` that resolves with an `ArrayBuffer` containing the entire contents of the `Blob` as binary data.
10+
11+
## Syntax
12+
13+
```js
14+
arrayBuffer()
15+
```
16+
17+
### Parameters
18+
19+
None.
20+
21+
### Return value
22+
23+
A `Promise` that resolves with an `ArrayBuffer` containing the blob's data.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
# Blob.size
8+
9+
The **`size`** read-only property of the `Blob` interface returns the size of the `Blob` in bytes.
10+
11+
## Value
12+
13+
A number representing the size of the `Blob` in bytes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
# Blob.slice()
8+
9+
The **`slice()`** method of the `Blob` interface creates and returns a new `Blob` object which contains data from a subset of the blob on which it's called.
10+
11+
## Syntax
12+
13+
```js
14+
slice()
15+
slice(start)
16+
slice(start, end)
17+
slice(start, end, contentType)
18+
```
19+
20+
### Parameters
21+
22+
- `start` _**optional**_
23+
- : The 0-based index of the first byte to include in the new `Blob`. If negative, it refers to an index from the end of the `Blob`. The default value is 0.
24+
- `end` _**optional**_
25+
- : The 0-based index of the first byte that will not be included in the new `Blob`. If negative, it refers to an index from the end of the `Blob`. The default value is `size`.
26+
- `contentType` _**optional**_
27+
- : A string indicating the content type to assign to the new `Blob`. The default value is an empty string.
28+
29+
### Return value
30+
31+
A new `Blob` object containing the specified subset of data.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
# Blob.stream()
8+
9+
The **`stream()`** method of the `Blob` interface returns a `ReadableStream` that can be used to read the contents of the `Blob`.
10+
11+
## Syntax
12+
13+
```js
14+
stream()
15+
```
16+
17+
### Parameters
18+
19+
None.
20+
21+
### Return value
22+
23+
A `ReadableStream` that provides the data contained within the `Blob`.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
# Blob.text()
8+
9+
The **`text()`** method of the `Blob` interface returns a `Promise` that resolves with a string containing the contents of the blob, interpreted as UTF-8.
10+
11+
## Syntax
12+
13+
```js
14+
text()
15+
```
16+
17+
### Parameters
18+
19+
None.
20+
21+
### Return value
22+
23+
A `Promise` that resolves with a string containing the blob's data as a text string. The data is always interpreted as UTF-8.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
# Blob.type
8+
9+
The **`type`** read-only property of the `Blob` interface returns the MIME type of the `Blob`.
10+
11+
## Value
12+
13+
A string indicating the MIME type of the `Blob`. If the type cannot be determined, this returns an empty string.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
# FormData()
8+
9+
The **`FormData()`** constructor creates a new `FormData` object.
10+
11+
## Syntax
12+
13+
```js
14+
new FormData()
15+
new FormData(form)
16+
```
17+
18+
> **Note:** `FormData()` can only be constructed with `new`. Attempting to call it without `new` throws a `TypeError`.
19+
20+
### Parameters
21+
22+
- `form` _**optional**_
23+
- : An HTML `<form>` element — when specified, the `FormData` object will be populated with the form's current key/value pairs using the name property of each element for the keys and their submitted value for the values. File input elements are handled specially: their values are taken from the files selected by the user in the upload control.
24+
25+
### Return value
26+
27+
A new `FormData` object, pre-populated with form data if the optional `form` parameter was provided.
28+
29+
## Description
30+
31+
The `FormData` interface provides a way to construct a set of key/value pairs representing form fields and their values, which can be sent using methods such as `fetch()`. It uses the same format a form would use if the encoding type were set to `"multipart/form-data"`.
32+
33+
You can also append additional data to the `FormData` object after it's created using its various methods.
34+
35+
A `FormData` object can be used in a number of ways with other APIs:
36+
37+
1. It can be sent with the `fetch()` API
38+
2. It works seamlessly with the `Request` and `Response` objects - it can be used directly as the body of a `Request` object
39+
3. It can be obtained from a `Response` object using the `formData()` method
40+
4. It can be passed directly to the `URLSearchParams` constructor
41+
42+
The transmitted data is in the same format that the form's `submit()` method would use to send the data if the form's encoding type were set to `"multipart/form-data"`.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
# FormData.append()
8+
9+
The **`append()`** method of the `FormData` interface appends a new value onto an existing key inside a `FormData` object, or adds the key if it does not already exist.
10+
11+
## Syntax
12+
13+
```js
14+
append(name, value)
15+
append(name, value, filename)
16+
```
17+
18+
### Parameters
19+
20+
- `name`
21+
- : The name of the field whose data is contained in `value`.
22+
- `value`
23+
- : The value of the field. This can be a string or a `Blob` (including subclasses such as `File`). If none of these, the value is converted to a string.
24+
- `filename` _**optional**_
25+
- : The filename reported to the server when a `Blob` or `File` is passed as the second parameter. The default filename for `Blob` objects is "blob". The default filename for `File` objects is the file's filename.
26+
27+
### Return value
28+
29+
`undefined`
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
# FormData.delete()
8+
9+
The **`delete()`** method of the `FormData` interface removes all key/value pairs with the given name from the `FormData` object.
10+
11+
## Syntax
12+
13+
```js
14+
delete(name)
15+
```
16+
17+
### Parameters
18+
19+
- `name`
20+
- : The name of the key you want to delete.
21+
22+
### Return value
23+
24+
`undefined`
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
# FormData.entries()
8+
9+
The **`entries()`** method of the `FormData` interface returns an iterator allowing iteration through all key/value pairs contained in this object. The iterator yields a new array for each key/value pair, with the first element being the key and the second element being the value.
10+
11+
## Syntax
12+
13+
```js
14+
entries()
15+
```
16+
17+
### Return value
18+
19+
An `iterator`.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
# FormData.forEach()
8+
9+
The **`forEach()`** method of the `FormData` interface executes the provided callback function once for each key/value pair in the `FormData` object.
10+
11+
## Syntax
12+
13+
```js
14+
forEach(callbackFn)
15+
forEach(callbackFn, thisArg)
16+
```
17+
18+
### Parameters
19+
20+
- `callbackFn`
21+
- : A function to execute for each entry in the object. The function will be passed the following arguments:
22+
- `value`
23+
- : The value of the current entry.
24+
- `key`
25+
- : The key of the current entry.
26+
- `formData`
27+
- : The `FormData` object being traversed.
28+
- `thisArg` _**optional**_
29+
- : A value to use as `this` when executing `callbackFn`.
30+
31+
### Return value
32+
33+
`undefined`
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
# FormData.get()
8+
9+
The **`get()`** method of the `FormData` interface returns the first value associated with a given key from within a `FormData` object. If you expect multiple values and want all of them, use the [`getAll()`](../../../globals/FormData/prototype/getAll.mdx) method instead.
10+
11+
## Syntax
12+
13+
```js
14+
get(name)
15+
```
16+
17+
### Parameters
18+
19+
- `name`
20+
- : The name of the key you want to retrieve.
21+
22+
### Return value
23+
24+
A `FormDataEntryValue` containing the value. If the key does not exist, it returns `null`.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
# FormData.getAll()
8+
9+
The **`getAll()`** method of the `FormData` interface returns all the values associated with a given key from within a `FormData` object.
10+
11+
## Syntax
12+
13+
```js
14+
getAll(name)
15+
```
16+
17+
### Parameters
18+
19+
- `name`
20+
- : The name of the key you want to retrieve.
21+
22+
### Return value
23+
24+
An array of `FormDataEntryValue` items containing all values with the given key. If the key doesn't exist, an empty array is returned.

0 commit comments

Comments
 (0)