How to return a variable from an attached form?

Asked

Viewed 66 times

0

''' I want my code to return a value/string from that form:

<form action="/dashboard/busca_inserir" method="POST" enctype="multipart/form-data">
<div>
  <label for="empresas_file">Insira um documento, caso exista</label><br>
  <input type="file" name="empresas_file" /><br><br>
  <input type="submit" id="empresas_file" value="Submeter Arquivo" />
</div>

Return function is down:

router.post('/busca_inserir', (req,res) => {

const empresas_file  = req.body.empresas_file;
console.log(empresas_file);
res.redirect('/dashboard/busca_inserir');});

Thus, the console log is always returning "Undefined" wanted to know how I get a value for this return.

1 answer

0

Because it is passing a file in the request, because of the enctype, it is necessary to read some npm package (at least it is one of the alternatives), maybe Multer or connect-multiparty.

For example:

npm install --save connect-multiparty

now, as middleware (The app is the object of the express)

const multiparty = require("connect-multiparty");
app.use(multiparty());

One more detail: use 'req.files.'

router.post('/busca_inserir', (req,res) => {

const empresas_file  = req.files.empresas_file;
console.log(empresas_file);
res.redirect('/dashboard/busca_inserir');});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.