Cody
Cody

Reputation: 919

Node/Multer | Can't get sperarte filename based on fieldname

I created an Express app in combination with multer to upload items in my Node.js app

What I try to do is select let's say:

Upload 1 - that has a fieldname of upfile1
Upload 2 - that has a fieldname of upfile2
Upload 3 - that has a fieldname of upfile3

basically, I need to select every uploaded filename item of my multi-upload app separately. Every upload needs to be handled differently in the app for different tasks. Let's use console.log as an example I need to do something like:

console.log(req.body.upfile1.filename);
console.log(req.body.upfile2.filename);
console.log(req.body.upfile3.filename);

to select the different items that get's handled in the app using different fieldname that are defined in my views using the name attribute.

below is my code

Views [index.html]

<form id="app-form" method="POST" class="fileupload" method="post" action="app" enctype="multipart/form-data">
<h1>Multi File Uploads</h1>
<input type="file" name="upfile1" value="">
<input type="file" name="upfile2" value="">
<input type="file" name="upfile3" value="">
<input type="submit" />
</form>

NodeJS [app.js]

app.get("/", function(req, res) {
  res.sendFile(__dirname + "/index.html");
});

app.post("/app", upload.any(), function(req, res) {
 let files = req.files;
 files.forEach(file => {
   console.log(file.filename);
});
 res.send(req.files);
 res.end();
});

Help would be very appreciated, thanks!

Upvotes: 0

Views: 941

Answers (1)

fardjad
fardjad

Reputation: 20394

Input elements in your markup must be wrapped in a form (they probably are already wrapped in a form element but not shown in your question). You should also set the form's enctype attribute to multipart/form-data.

<form method="post" enctype="multipart/form-data" action="/upload">
    <input type="file" name="upfile1">
    <input type="file" name="upfile2">
    <input type="file" name="upfile3">

    <input type="submit" value="Submit">
</form>

Once that's done you can configure multer and create a route to handle file uploads:

const upload = multer({
  dest: path.join(__dirname, './upload') // You might want to change this according to your preferences
  // Since you're using any(), you might want to set fileFilter to control which files should be uploaded. See: https://github.com/expressjs/multer#filefilter
});

const findFileByFieldname = (files, fieldname) => {
  return files.find(file => file.fieldname === fieldname) || {};
}

app.post("/upload", upload.any(), (req, res) => {     
  const upfile1Filename = findFileByFieldname(req.files, 'upfile1').filename;
  const upfile2Filename = findFileByFieldname(req.files, 'upfile2').filename;
  const upfile3Filename = findFileByFieldname(req.files, 'upfile3').filename;

  res.json({
    upfile1Filename,
    upfile2Filename,
    upfile3Filename,
  });
});

// Example response (Node v8.11.4, Express v4.16.3, Multer v1.3.1)
// {"upfile1Filename":"360726b532a01b0e31832f067b5922c8","upfile2Filename":"144e1298437afb51f36eb37c77814650","upfile3Filename":"4c908da20e770130377e4006db945af6"}

Upvotes: 1

Related Questions