Trying to upload multiple images, but can get only one uploaded,

LeoJohn
LeoJohn Member Posts: 3


const Upload = () => {

  async function handleOnSubmit(event: React.FormEvent<HTMLFormElement>) {

    event.preventDefault();

    const form = event.currentTarget;

    const fileInput = form.elements.namedItem('getImage') as HTMLInputElement;

    const formData = new FormData();

    let arrayFiles;

    if (fileInput)

      arrayFiles = fileInput.files ? Array.from(fileInput.files) : [];

    if (arrayFiles){

      for (const file of arrayFiles) {

        formData.append('file', file);

      }

    }

    formData.append('upload_preset', 'leave blank here');

    console.log(formData.getAll("file"));

    const data = await fetch('https://api.cloudinary.com/v1_1/[ i leave blank here ]/image/upload', {

      method: 'POST',

      body: formData

    }).then(r => r.json());

  }

  return (

    <form  onSubmit={handleOnSubmit}>

      <input name="getImage" type="file" placeholder='pick files' multiple accept='image/jpeg,image/png,image/webp,image/gif,video/mp4,video/quicktime' />

      <button type="submit">upload</button>

    </form>

  )

}

export default Upload

Tagged:

Best Answer

Answers