balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.83k stars 1.95k forks source link

Sails not detecting multiple file upload #4364

Open LiamMartens opened 6 years ago

LiamMartens commented 6 years ago

Sails version: 1.0.1 Node version: 9.11.1 NPM version: 5.6.0 DB adapter name: N/A DB adapter version: N/A Operating system: Alpine Linux


It appears sails is not handling the body parsing correctly when trying to upload multiple files. Either when uploading more than 1 file with different keys or when uploading an array of files. I have tried so using Insomnia REST client but also using raw cURL requests as detailed below.

Either way I am never able to retrieve the 2nd (or subsequent file) using the this.req.file('fieldName') in a controller action, or the file array only contains 1 raw file (this.req.file('field')._files)

Note I did send the text fields before the file fields as detailed in the documentation

Example cURL

curl -X POST -F "name=someName" -F "image=@/path/to/image" -F "other=@/path/to/other" http://localhost:1337/upload

This results in only being able to get a file in this.req.file('image').

curl -X POST -F "name=someName" -F "image[]=@/path/to/image" -F "image[]=@/path/to/other" http://localhost:1337/upload

This results in getting no files in this.req.file('image')._files and only getting 1 file in this.req.file('image[]')._files.

sailsbot commented 6 years ago

Hi @LiamMartens! It looks like you missed a step or two when you created your issue. Please edit your comment (use the pencil icon at the top-right corner of the comment box) and fix the following:

As soon as those items are rectified, post a new comment (e.g. “Ok, fixed!”) below and we'll take a look. Thanks!

*If you feel this message is in error, or you want to debate the merits of my existence (sniffle), please contact inquiries@sailsjs.com

LiamMartens commented 6 years ago

Fixed

sailsbot commented 6 years ago

@LiamMartens Thanks for posting, we'll take a look as soon as possible.


For help with questions about Sails, click here. If you’re interested in hiring @sailsbot and her minions in Austin, click here.

mikermcneil commented 6 years ago

@LiamMartens thanks for posting Liam. Would you have a look at https://trello.com/c/WUyTNbx8? (And to a lesser degree, also: https://trello.com/c/WA2Kbtcd)

That should resolve your issue, but let me know if not and I'll take a closer look

mikermcneil commented 6 years ago

@LiamMartens Actually, re-reading your issue, I'm not sure this is the problem. You'll need to wait until req.file(…).upload(…) is complete -- i.e. in the callback -- before checking what files were uploaded.

Here's an example using async/await from the recent Sails 1.0 tutorial course.

For multiple files, just change that to var uploads = sails.upload(…)

Also, here's another, more classic example, using callbacks, in case you're not using actions2

LiamMartens commented 6 years ago

@mikermcneil about the callback, I know and the thing is it only uploads 1 file either way. Besides the raw files array req.file(...)._files also never contains more than a singular file for some reason and it should contain the headers and other metadata from all the files that were uploaded on that key. I believe it is a multipart/form-data parsing issue in Skipper maybe?

mikermcneil commented 6 years ago

Definitely possible- software is a cruel mistress

Would you create a repo that reproduces this and link to it from here?