简体   繁体   English

使用 fs 在 multer 中重命名上传的图像

[英]Rename uploaded image in multer using fs

When a user uploads a picture - I'm using multer by the way - with a name that contains a space, I want to replace the white-spaces with "-" before saving the file name to my Mysql table and then also rename the file that has been uploaded to the new name with the "-" .当用户上传图片时 - 顺便说一下,我正在使用 multer - 名称包含空格,我想在将文件名保存到 Mysql 表之前用"-"替换空格,然后重命名已上传到带有"-"的新名称的文件。 This is my code.这是我的代码。 It does rename and the Mysql table saves the new name with the hyphen but the image doesn't get uploaded for some reason, there's no error even, infact the POST request is actually successful.它确实重命名并且 Mysql 表用连字符保存了新名称,但由于某种原因图像没有上传,甚至没有错误,事实上 POST 请求实际上是成功的。

router.post('/uploadpost', upload.single('image'), function(req, res){
var name = req.file.originalname;
var reName;
if(/\s/g.test(name)){
    reName = name.replace(" ", "-");
    fs.rename(req.file.path, reName, function(err){
        if(err){
            throw err;
        }

    });
}

Why not change the name when you save the file by Multer?当您通过 Multer 保存文件时,为什么不更改名称? Something like that:类似的东西:

function generateName(){
   return 'newname';
}

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, '/tmp/my-uploads')
  },
  filename: function (req, file, cb) {
    cb(null, generateName()); // generate new name 
  }
})

var upload = multer({ storage: storage })

Documentation - https://github.com/expressjs/multer#diskstorage文档 - https://github.com/expressjs/multer#disktorage

You can Change file names easily in multer like hown below您可以在multer轻松更改文件名,如下所示

const multer = require('multer');
let fs = require('fs-extra');

let storage = multer.diskStorage({
    destination: function (req, file, cb) {
        let path = `tmp/upload`;
        fs.mkdirsSync(path);
        cb(null, path);
    },
    filename: function (req, file, cb) {
        console.log(file);
        cb(null, file.fieldname + '-' + Date.now() + "." + extension);
    }
})

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

in above code I am giving file name like this file.fieldname fieldname is name of field using which you are sending images like fieldname could be "image" or anything and than - , Date.now() the date when you are uploading image and in last type or extension of file ex images-1602486722326.jpeg在上面的代码中,我给出了这样的文件名file.fieldname fieldname 是您发送图像的字段名称,例如 fieldname 可以是"image"或任何东西,而不是 - , Date.now()上传图像的日期和在文件 ex images-1602486722326.jpeg最后一种类型或extension

OR或者

you can keep original name of image if you want like below如果你想像下面这样,你可以保留图像的原始名称

 filename: function (req, file, callback) {
        callback(null, file.originalname);
    }

this will get originalame of file and give it to the file which you are uploading UPDATE AND in MemoryStorage这将获得文件的原始名称并将其提供给您正在上传UPDATE和 MemoryStorage 的文件

const upload = multer({
    storage: multerS3({
        s3: s3,
        bucket: 'bucket_name',
        acl: 'public-read',
        key: function (request, file, cb) {
            console.log(file);    |--------------  change name here which you want to use
                                  |              
            cb(null, file.originalname);  use
        }
    })
}).array('upload', 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM