Ad

Filepond Process All Files In One Request

I am using react-filepond to process multiple files. I need to send all files together to the API in one request as opposed to multiple request per each file:

// Import the Image EXIF Orientation and Image Preview plugins
import FilePondPluginImageExifOrientation from 'filepond-plugin-image-exif-orientation'
import FilePondPluginImagePreview from 'filepond-plugin-image-preview'
import 'filepond-plugin-image-preview/dist/filepond-plugin-image-preview.css'

// Register the plugins
registerPlugin(FilePondPluginImageExifOrientation, FilePondPluginImagePreview)

function App() {
  console.log('TEST');
  const [files, setFiles] = useState([])
  console.log(files);
  return (
    <div className="App">
      <FilePond
        server='/api'
        files={files}
        allowMultiple={true}
        onupdatefiles={setFiles}
        labelIdle='Drag & Drop your files or <span class="filepond--label-action">Browse</span>'
      />
    </div>
  )
}

const rootElement = document.getElementById('root')
ReactDOM.render(<App />, rootElement)

Sandbox link here.

How can I implement this?

Ad

Answer

Don't use the server property. You can map your files array to file objects, files.map(item => item.file), then you pass the file items to a FormData object and send the formData object to the server.

const formData = new FormData();
files.map(item => item.file).forEach(file => formData.append('my-file', file))

// send formData to server with `fetch` or `XMLHttpRequest`
Ad
source: stackoverflow.com
Ad