I can’t find any documentation on file uploads for Netlify forms using fetch for submission.
I am using Formik with yup for validation and need to send a form with a range of fields, including one field with multiple file uploads.
The form is successfully posting to Netlify with all other data using Object.keys, encodeURIComponent and .join(“&”). But this won’t work for Files as it needs to send the File object. Any idea how I can send this data along with other fields so that Netlify recognises and has access to the files from the Form alongside this data?
Hi @danwebb, while there is no documentation that specifically covers file uploads for react apps, we do have an example in a repo from the author of that article. Let me know if that helps.
Using that repo as a guide, I was able to get a ‘single’ File sent after validation. However I can’t seem to get Netlify to recognise multiple files within the Filelist object that is sent for a single field. I even tried creating a new array with the given File objects instead, but the response for that field Netlify side is then blank:
function encode(data) {
const formData = new FormData();
So I guess the question is in what format does Netlify accept ‘multiple’ File uploads for a single field. Or does it only accept single File uploads? And therefore I’d need to create 3 fields for single File uploads?
I wouldn’t expect any changes to this behavior anytime in the near future. As promised, we will follow up here if things ever do change, which is definitely possible but not at all guaranteed.
For anyone else wondering how to handle multiple file uploads with Netlify Forms, if you’re using React you can use a combination of react-dropzone and JSZip to zip the files client side. Here’s the basic idea that I just got working on a site…
First, react-dropzone has an onDrop callback…
<Dropzone onDrop={this.handleDrop}>
{({ getRootProps, getInputProps }) => (
<section>
<div {...getRootProps()}>
<input {...getInputProps()} name="files" />
<p>Drag n' drop some files here, or click to select files</p>
</div>
</section>
)}
</Dropzone>
It was used on a gated site that’s only accessible upon receiving an email invite - and I’m not at liberty to send those invites to just anyone I’m afraid. But the code above was pretty similar to what I used in production - the only change I ended up making was changing the file type in the handleDrop function from blob to uint8array…
I’m looking into using file uploads for the first time and noticed the comment above about the unstated limit of one. As far as I can see, that is still not documented. Can that be prioritized? Unless multifile support is around the corner of course.
Has anyone found a solution in pure HTML and JS? I would really like to have a <input type="file" multiple> and have multiple URL-s directly on the forms page without having to worry about third-party file senders etc.