2

I am trying to send an array of images to my backend and then upload them with multer-s3. I am sending them through react to a node express backend. When I submit from the frontend it is a submitting an array of images like so:

images : [ File(3195869) {name: "IMG - example.JPG", lastModified: 1514505624000, lastModifiedDate: Thu Dec 28 2017 17:00:24 GMT-0700 (MST), webkitRelativePath: "", size: 3195869, …}, ... ]

But when I log req.body on the backend it returns [ {}, {}, {}, ... ] and req.files is undefined

Why would the files not be making it through the post request??

Nick McKendry
  • 21
  • 1
  • 3

2 Answers2

0

It is explained in the Usage section ^^

app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {
  // req.files is array of `photos` files 
  // req.body will contain the text fields, if there were any 
})

Like express js says :

"In Express 4, req.files is no longer available on the req object by default. To access uploaded files on the req.files object, use multipart-handling middleware like busboy, multer, formidable, multiparty, connect-multiparty, or pez."

A good tutorial for you, with the part where you want to upload multiple files : https://scotch.io/tutorials/express-file-uploads-with-multer#toc-upload-multiple-files

jy95
  • 773
  • 13
  • 36
  • I meant to include that as part of my question, but req.files is undefined as well – Nick McKendry Jan 03 '18 at 22:42
  • Try to look at this issue : https://github.com/expressjs/multer/issues/203 (and the latest answer : https://github.com/expressjs/multer/issues/203#issuecomment-348856217) . Like express js says : "In Express 4, req.files is no longer available on the req object by default. To access uploaded files on the req.files object, use multipart-handling middleware like busboy, multer, formidable, multiparty, connect-multiparty, or pez." – jy95 Jan 04 '18 at 09:35
  • Ok, so I think my issue is that I'm not uploading straight from the input but I am storing the photos in a store and then on the final step I am trying to send the photos to s3, and multer is looking for an input name but they're not coming from an input when a user hits submit. Thanks for the help! – Nick McKendry Jan 04 '18 at 17:53
0

I have used express-form-post from npm and found it quite useful. You can access that file array with Object.keys(req.files[index]) in some for loop or sumn and do whatever u want with them after..... its very compatible with s3... the name attribute from the form field is the key and other metadata(file size, original name, etc) is the data that goes along with that key