Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store a file with file extension with multer?

Managed to store my files in a folder but they store without the file extension.

Does any one know how would I store the file with file extension?

like image 473
user3355603 Avatar asked Jul 23 '15 16:07

user3355603


People also ask

Where does multer save file?

Before using Multer to handle the upload action of files, we need to understand a few things. The actual files are never stored in the database. They are always stored someplace on the server. In our tutorial, we will store the uploaded files in the public folder.

How do I use multer to file?

The following code will go in the app.const multer = require('multer'); const upload = multer({dest:'uploads/'}). single("demo_image"); Here, we have called the multer() method. It accepts an options object, with dest property, which tells Multer where to upload the files.

Is multer filename unique?

So, it does not depend on how many files you store but how fast requests come to your system. If you have more than 1000 requests per second, there will be a chance for duplication. In this case, you should use something like UUID.


3 Answers

I have a workaround for the adding proper extension of files. If you use path node module

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

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'uploads/')
  },
  filename: function (req, file, cb) {
    cb(null, Date.now() + path.extname(file.originalname)) //Appending extension
  }
})

var upload = multer({ storage: storage });
like image 155
sree Avatar answered Oct 20 '22 12:10

sree


From the docs: "Multer will not append any file extension for you, your function should return a filename complete with an file extension."

Here's how you can add the extension:

var multer = require('multer');

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'uploads/')
  },
  filename: function (req, file, cb) {
    cb(null, Date.now() + '.jpg') //Appending .jpg
  }
})

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

I would recommend using the mimetype property to determine the extension. For example:

filename: function (req, file, cb) {
  console.log(file.mimetype); //Will return something like: image/jpeg

More info: https://github.com/expressjs/multer

like image 54
Scott Avatar answered Oct 20 '22 14:10

Scott


I got file the extension from file.mimetype . I split the mimetype and get the file extension from it Please try the below function.

let storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, './uploads')
  },
  filename: function (req, file, cb) {
    let extArray = file.mimetype.split("/");
    let extension = extArray[extArray.length - 1];
    cb(null, file.fieldname + '-' + Date.now()+ '.' +extension)
  }
})
const upload = multer({ storage: storage })
like image 18
VISHNU Avatar answered Oct 20 '22 14:10

VISHNU