0

I am already able to upload video files to AWS S3, but I want to also have the user upload a thumbnail img along with the video they select to upload. Is there a way to upload multiple files at once using multer and mongoDB?

enter image description here

enter image description here

enter image description here

enter image description here

Alyx
  • 17
  • 7

1 Answers1

1

Of course you can! first, you must use upload.array('file', 2) in your api(the 2 here is the number of files to upload), and in your react code you need an input that accepts multiple files, like this: <input type='file' multiple='multiple' />

One important step is to make the post as multipart/form-data and send the array of files to the server, maybe in your handleChange function you can do this: this.setState({[key]: e.target.files }); to send all files at once. One last thing, maybe you want to store just the url to the file in your mongodb, if so, then change the type to string, file: {type: String} and thumbnail: {type: String}. Hope i clarified the things a bit!

some good references for you:

Uploading multiple files with Multer

https://www.npmjs.com/package/multer

Yago Biermann
  • 1,494
  • 1
  • 7
  • 20
  • Thanks for responding! Just tried doing what you suggested and file is now coming back as undefined. Im very new to react, aws, and stackoverflow. How can I share my screenshots with you? – Alyx Mar 29 '22 at 13:57
  • You're welcome! but please, try to post your question as complete as possible that way i can help you more, edit your question and add your code with the code sample tool, screenshots aren't helpful. I also need to see the code where you are getting the files from the user and the code where you are making the api call, like axios or fetch. – Yago Biermann Mar 29 '22 at 16:59