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.

Files are uploading as 'file' without its extension

See original GitHub issue

Hi,

i am using multer to upload my images. Uploading works fine but the problem is all my files are uploading with ‘file’ filetype ie, without its extension. can anyone tell me why…?

My Code:

var uploadProfileImgs = multer({dest : './files/uploads/profile/'}).single('avatar');

app.post('/profile', function (req, res) {
  uploadProfileImgs(req, res, function (err) {
    if (err) {
      console.log(err.message);
      // An error occurred when uploading
      return
    }
    console.log('Everything went fine');
    // Everything went fine
  })
})

My files looks like below: w

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:40 (14 by maintainers)

github_iconTop GitHub Comments

285reactions
rmoura-92commented, Jul 21, 2015

my workaround:

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, './uploads/')
  },
  filename: function (req, file, cb) {
    crypto.pseudoRandomBytes(16, function (err, raw) {
      cb(null, raw.toString('hex') + Date.now() + '.' + mime.extension(file.mimetype));
    });
  }
});
var upload = multer({ storage: storage });
90reactions
LinusUcommented, Jul 21, 2015

As @gabeio said this is the default behaviour from version 1.0.0 and forward.

@ricardomoura You can supply destination as a string if you want to. And if you don’t want to include the mime library you could look at the extension of the original file using the builtin module path.

var path = require('path')
var multer = require('multer')

var storage = multer.diskStorage({
  destination: './uploads/',
  filename: function (req, file, cb) {
    crypto.pseudoRandomBytes(16, function (err, raw) {
      if (err) return cb(err)

      cb(null, raw.toString('hex') + path.extname(file.originalname))
    })
  }
})

var upload = multer({ storage: storage })

Attention Lookout when doing this thought as the client can send any file extension that they want, regardless of what the file actually contains. A rule of thumb is to never trust any data from the client.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Open a File With No Extension
You should first confirm if your file actually has no extension, or you just don't have the program needed to open a particular...
Read more >
Is it safe to accept a file upload without any file extension?
The extension itself should not matter. It would be good to figure out the MIME type to know about the content instead.
Read more >
How to open file with no extension in Windows 11/10
After uploading the file, click on the Check File Type button. A new tab will open, where the extension of the uploaded file...
Read more >
Forums - How to accept uploading files WITHOUT extension?
You're not getting the problem. I'm trying to deal with files WITHOUT any extension. I think that your code is changing the name...
Read more >
How to Change the Allowed File Types in the File Upload ...
The default file extensions that Jotform accepts are:... ... The uploaded files are not attaching to the email that I receive.
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