如何使用multer存储文件扩展名的文件?

use*_*603 26 node.js npm express multer

管理将我的文件存储在文件夹中,但它们存储时没有文件扩展名.

有没有人知道如何存储文件扩展名的文件?

sri*_*hks 65

对不起,我无法对Scott上面的答案发表评论,但我有一个解决方法,可以添加正确的文件扩展名.如果使用path节点模块

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 });
Run Code Online (Sandbox Code Playgroud)

  • 这是最好的解决方案 (4认同)
  • 此解决方案有一个问题,它不考虑没有文件扩展名的文件。在这种情况下,您需要使用文件的 mime 类型。 (3认同)
  • 正确的答案,应该具有动态文件命名。 (2认同)

Sco*_*ott 35

从文档:"Multer不会为您附加任何文件扩展名,您的函数应该返回一个带有文件扩展名的文件名."

以下是添加扩展程序的方法:

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 });
Run Code Online (Sandbox Code Playgroud)

我建议使用该mimetype属性来确定扩展名.例如:

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

更多信息:https://github.com/expressjs/multer

  • 这个答案只是指出 Multer 不附加文件扩展名,但 OP 要求一种附加扩展名的方法。该建议是有效的,但没有回答主要问题。 (2认同)

小智 9

我收到了扩展文件file.mimetype.我拆分mimetype并从中获取文件扩展名请尝试以下功能.

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 })
Run Code Online (Sandbox Code Playgroud)

  • 如果您尝试上传 .docx 文件,则此解决方案会出现问题,但此方法会失败 (2认同)

Zoh*_*lam 5

可以这样做:

var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, config.DIR)
    },
    filename: function (req, file, cb) {
        let ext = file.originalname.substring(file.originalname.lastIndexOf('.'), file.originalname.length);
        cb(null, Date.now() + ext)
    }
});
const upload = multer({
    storage: storage
}).any();
Run Code Online (Sandbox Code Playgroud)


Dan*_*tie 5

我使用这个小技巧来获取文件扩展名,并作为解决方法来避免有人上传具有相似文件名的文件两次或服务器中存在的文件时可能出现的问题。

const path = require('path');
const crypto = require('crypto');

let upload = multer({
storage: multer.diskStorage({
    destination: (req, file, cb) => {
        cb(null, path.join(__dirname, '../uploads'))
    },
    filename: (req, file, cb) => {
        // randomBytes function will generate a random name
        let customFileName = crypto.randomBytes(18).toString('hex')
        // get file extension from original file name
        let fileExtension = path.extname(file.originalname).split('.')[1];
        cb(null, customFileName + '.' + fileExtension)
    }
  })
})
Run Code Online (Sandbox Code Playgroud)


m.e*_*roy 5

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}`);
  }
});
Run Code Online (Sandbox Code Playgroud)

编辑

shortid 已被弃用,您应该使用 nanoid。

import multer from 'multer';
import * as nanoid from 'nanoid';
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 = nanoid();
    /* 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}`);
  }
});
Run Code Online (Sandbox Code Playgroud)