Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multer: how to name files after req.body parameters #134

Closed
ayhoung opened this issue May 4, 2015 · 4 comments
Closed

Multer: how to name files after req.body parameters #134

ayhoung opened this issue May 4, 2015 · 4 comments

Comments

@ayhoung
Copy link

ayhoung commented May 4, 2015

I'm trying to upload a file with a form such as below

<input type="file" name="collateral" />
<input type="hidden" name="id" value="ABCDEFG" />
<input type="submit" value="Upload Image" name="submit">

and I would like to rename to file to the name in the id input (ABCDEFG). As I can't access the req.body through the rename: function(fieldname, filename), I was wondering how I would achieve this?

@ayhoung
Copy link
Author

ayhoung commented May 4, 2015

the documentation also states that accessing req.body is not allowed in this function, so any idea of how this would be achieved?

@LinusU
Copy link
Member

LinusU commented May 4, 2015

Using the code in #131 you should be able to read req.body, if the browser sends it before the file. The easiest way to achieve that would be to place the hidden field above the file input.

Actually it probably should work already, as long as you don't use any brackets in the field names.

@sparkyfen
Copy link

I noticed this as well.
Sending the fields first allows my rename to trigger as expected. I will get 1111111111 for the onFileUploadStart function.
screen shot 2015-05-06 at 4 22 19 pm
screen shot 2015-05-06 at 4 23 19 pm

If they are file first, then field, I get undefined within the onFileUploadStart function.

@LinusU
Copy link
Member

LinusU commented May 18, 2015

This is how it will always be since we can't change the order in which the browsers sends the data.

If you cannot place the file-input later in the DOM, you could use javascript on the client side to construct your own FormData with the files added last.

@LinusU LinusU closed this as completed May 18, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants