question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to return filename?

See original GitHub issue

I would like to return the randomly generated file name back so that the front end can store the filename in the db, but I can’t figure out how to use the callback to return the filename to somewhere I can use res.send to send it out. Here’s my code:

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, '../images/');
  },
  filename: function (req, file, cb) {
    var id = util.random_string(16) + Date.now() + path.extname(file.originalname);
    cb(null, id);
  }
});
var upload = multer({ storage: storage }).single('image');

router.post('/image/', function (req, res) {
  upload(req, res, function (err) {
    if (err){
      console.log(JSON.stringify(err));
      res.status(400).send("fail saving image");
    } else {
      res.send('how can I return the file name?');
    }
  });
});

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:7
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

27reactions
vanhooferwincommented, Nov 2, 2017

Hi Louis,

For people who are struggling with this issue.

The filename is in the res.req.file.filename variable. In your code it would look like this:

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, '../images/');
  },
  filename: function (req, file, cb) {
    var id = util.random_string(16) + Date.now() + path.extname(file.originalname);
    cb(null, id);
  }
});
var upload = multer({ storage: storage }).single('image');

router.post('/image/', function (req, res) {
  upload(req, res, function (err) {
    if (err){
      console.log(JSON.stringify(err));
      res.status(400).send('fail saving image');
    } else {
      console.log('The filename is ' + res.req.file.filename);
      res.send(res.req.file.filename);  
    }
  });
});
20reactions
LinusUcommented, Jan 20, 2016

Look under “File information” in the documentation.

req.file.filename
Read more comments on GitHub >

github_iconTop Results From Across the Web

Insert the current Excel file name, path, or worksheet in a cell
Insert the current file name and the name of the active worksheet · To enter a formula as an array formula, press CTRL+SHIFT+ENTER....
Read more >
Get File Name / Excel Formula - Excel Champs
How to Get File Name in Excel · First, use the SEARCH function to know the position of the starting square bracket by...
Read more >
Insert File Name - Excel Formula
File Name, Path, and Worksheet​​ We use the CELL Function to return the file path, name, and sheet by entering “filename” as the...
Read more >
How to Get filename in Excel
In this article, we will learn How to Get filename in Excel. Scenario: Working with excel file information using excel formula is easy,...
Read more >
How to Extract filename from a given path in C# ...
A path may contain the drive name, directory name(s) and the filename. To extract filename from the file, we use “GetFileName()” method of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found