Misha Goldstein
Misha Goldstein

Reputation: 21

How to submit a form with a file and text inputs at the same time

I am using Nodejs, Express, MySQL, Bodyparser and EJS.

I am using MULTER for the file upload.

Hello, I have a form which requires a file to be uploaded and text to be written in the same form.

I can't get the file and the text to both work at the same time. I haven't been able to find an ENCTYPE attribute which fits my needs.

It looks something like this:

const bodyParser = require("body-parser"); app.use(bodyParser.urlencoded({ extended: true }));

<form action="/" method="post">
    <input type="file" name="image" id="">
    <input type="text" name="textone" id="">
    <input type="text" name="texttwo" id="">
    <input type="submit" value="submit">
</form>

Using ENCTYPE multipart/form-data allows the file to be uploaded succesfully but not the text, while using ENCTYPE application/x-www-form-urlencoded allows the text to work but not the file.

Online it says that I should be able to get the text input values using the multipaprt/form-data ENCTYPE but I can't, It's not found in the req.body( I also tried req.textone because the file is stored as "req.file")

How can I fix this?

Thank you

Upvotes: 1

Views: 644

Answers (1)

Misha Goldstein
Misha Goldstein

Reputation: 21

I just found the answer, the problem was that I was trying to req.body.textone OUTSIDE of the upload function, when I placed a console.log INSIDE of the upload function, it gave me the correct one.

Upvotes: 1

Related Questions