How to store a file with file extension with multer?

后端 未结 11 1636
情话喂你
情话喂你 2020-12-13 17:56

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?

11条回答
  •  猫巷女王i
    2020-12-13 18:12

    import multer from 'multer';
    import * as shortid from 'shortid';
    import * as mime from 'mime-types';
    
    const storage = multer.diskStorage({
      destination: function (req,file,cb) {
        cb(null, '/path/to/uploads/');
      },
      filename: function (req,file,cb) {
        /* generates a "unique" name - not collision proof but unique enough for small sized applications */
        let id = shortid.generate();
        /* need to use the file's mimetype because the file name may not have an extension at all */
        let ext = mime.extension(file.mimetype);
        cb(null, `${id}.${ext}`);
      }
    });
    

提交回复
热议问题