本文介绍了用multer重命名文件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在尝试使用multer重命名文件.我想重命名上传到.jpg的文件而不是有关multer的教程,我在路由文件中调用方法rename.该文件已很好地上传,但我不明白为什么函数重命名无法正常工作.顺便说一句,"ici"一词没有出现在我的控制台中

I'm trying to rename a file using multer. I want to rename the file uploaded to a .jpgInstead of the tutorial about multer, i'm calling the method rename in my route file. The file is well uploaded but I don't get why the function rename is not working.By the way, the word 'ici' doesn't appear into my console

router.post('/edit/saveEdit',multer({
rename : function(fieldname, filename, req, res) {
    console.log('ici');
    return req.body.infoUser.id
}}),
function(req,res){

// console.log(req.body);
// console.log(JSON.stringify(req.files));

var conf = JSON.parse(fs.readFileSync(file_user));
var user = req.body.infoUser;


//changement de nom de fichier
// console.log(req.files.uploadAvatar);

感谢您的回答/帮助锡伯特

Thanks for answers/helpThibault

res.end('good');

推荐答案

它适用于我,代码如下:

It is working for me with the code as below:

    var multer  = require('multer')
    var storage = multer.diskStorage({
        destination: function (req, file, cb) {
            cb(null, 'public/uploads/')
        },
        filename: function (req, file, cb) {

            var getFileExt = function(fileName){
                var fileExt = fileName.split(".");
                if( fileExt.length === 1 || ( fileExt[0] === "" && fileExt.length === 2 ) ) {
                    return "";
                }
                return fileExt.pop();
            }
            cb(null, Date.now() + '.' + getFileExt(file.originalname))
        }
    })

    var multerUpload = multer({ storage: storage })
    var uploadFile = multerUpload.single('file');

这篇关于用multer重命名文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 16:15