diff --git a/docs/developer-docs/latest/plugins/upload.md b/docs/developer-docs/latest/plugins/upload.md index ba4b910206..e3729d9ed5 100644 --- a/docs/developer-docs/latest/plugins/upload.md +++ b/docs/developer-docs/latest/plugins/upload.md @@ -107,13 +107,15 @@ module.exports = ({ env }) => ({ ## Upload files -To upload files into your application. +To upload files to your application. ### Parameters - `files`: The file(s) to upload. The value(s) can be a Buffer or Stream. -### Code example + + + ```html
@@ -123,20 +125,48 @@ To upload files into your application.
``` +
+ + + +```js +import { FormData, Blob } from "formdata-node" +import { FormDataEncoder } from "form-data-encoder" +import { Readable } from "stream" +import fetch from 'node-fetch'; +import fs from 'fs'; + +const file = fs.createReadStream('path-to-your-file'); +const form = new FormData(); + +form.append('files', file); + +const encoder = new FormDataEncoder(form); + +await fetch('http://localhost:1337/api/upload', { + method: "post", + headers: encoder.headers, + body: Readable.from(encoder) +}); +``` + + + +
+ :::caution You have to send FormData in your request body. ::: @@ -189,16 +219,15 @@ Code ``` @@ -245,44 +274,36 @@ Code ``` -Your entry data has to be contained in a `data` key. You have to `JSON.stringify` your data object. - -And for your files, they have to be prefixed by `files`. -Example here with cover attribute `files.cover`. +Your entry data has to be contained in a `data` key and you need to `JSON.stringify` this object. The keys for files, need to be prefixed with `files` (example with a cover attribute: `files.cover`). ::: tip If you want to upload files for a component, you will have to specify the index of the item you want to add the file to.