How to get filename in Multer?

by ola.stoltenberg , in category: JavaScript , a year ago

How to get filename in Multer?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by mortimer , a year ago

@ola.stoltenberg 

Multer is a middleware that allows you to handle HTTP requests with enctype "multipart/form-data", which is used for file uploads.


To get the filename of the uploaded file in Multer, you can use the .file or .files properties of the request object, depending on whether you are handling a single file or multiple files.


Here is an example of how you can get the filename of a single file uploaded using Multer:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
const multer = require('multer');

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, '/tmp/my-uploads');
  },
  filename: function (req, file, cb) {
    cb(null, file.originalname);
  }
});

const upload = multer({ storage: storage });

app.post('/upload', upload.single('myFile'), (req, res) => {
  console.log(req.file.filename); // This will contain the file's filename
  res.send('File uploaded');
});


If you are handling multiple files, you can use the .files property of the request object, which is an array of the uploaded files. Here is an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
const multer = require('multer');

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, '/tmp/my-uploads');
  },
  filename: function (req, file, cb) {
    cb(null, file.originalname);
  }
});

const upload = multer({ storage: storage });

app.post('/upload', upload.array('myFiles'), (req, res) => {
  req.files.forEach(file => {
    console.log(file.filename); // This will contain the filename of each uploaded file
  });
  res.send('Files uploaded');
});


Note that the .file and .files properties are only available after the file(s) have been successfully uploaded and processed by Multer.

Member

by fae , 3 months ago

@ola.stoltenberg 

To get the filename in Multer, you can use the req.file.filename or req.files[i].filename property depending on whether you are handling a single file or multiple files.


Here's an example for getting the filename for a single file upload:

1
2
3
4
5
6
7
const multer = require('multer');
const upload = multer({ storage: storage });

app.post('/upload', upload.single('myFile'), (req, res) => {
  console.log(req.file.filename); // This will contain the file's filename
  res.send('File uploaded');
});


And here's an example for getting the filenames for multiple file uploads:

1
2
3
4
5
6
7
8
9
const multer = require('multer');
const upload = multer({ storage: storage });

app.post('/upload', upload.array('myFiles'), (req, res) => {
  req.files.forEach(file => {
    console.log(file.filename); // This will contain the filename of each uploaded file
  });
  res.send('Files uploaded');
});


Note that in both examples, storage is a multer.diskStorage instance which defines the destination and filename configuration for storing the uploaded files. You can customize the destination and filename functions according to your requirements.