Skip to content

Commit

Permalink
Add example usage with files and blobs
Browse files Browse the repository at this point in the history
  • Loading branch information
octet-stream committed Mar 12, 2024
1 parent bc906fe commit 4c4659c
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,34 @@ Result:
[4] = "pomegranate"
```

5. Serializing files and blobs:

```ts
import {objectToFormData} from "@octetstream/object-to-form-data"

const input = [
{
caption: "Text file created with File object",
file: new File(["My hovercraft if full of eels"], "test.txt", {type: "text/plain"}),
},
{
caption: "Text data created with Blob object",
file: new Blob(["On Soviet Moon landscape see binoculars through you"], {type: "text/plain"}),
}
]

const form = objectToFormData(input)
```

Result:

```
[0][caption] = "Text file created with File object"
[0][file] = File [type: "text/plain", name: "test.txt", content: "My hovercraft if full of eels"]
[1][caption] = "Text data created with Blob object"
[1][file] = File [type: "text/plain", name: "blob", content: "On Soviet Moon landscape see binoculars through you"]
```

## API

### `objectToFormData(input[, options]): FormData`
Expand Down

0 comments on commit 4c4659c

Please sign in to comment.