Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

multer rename file

const multer = require('multer')

const addZero = (i: string | number) => {
    if (i < 10) {
        i = "0" + i;
    }
    return i;
}

const upload = multer({
    storage: multer.diskStorage({
        destination: EMS_ITEM_MASTER_FILE_FOLDER,
        filename: function (req, file, cb) {
            let originalFileName = file.originalname;
            let originalFileFrgs = originalFileName.split(".");

            const today = new Date();
            const fullYear = today.getFullYear();
            const month = addZero(today.getMonth());
            const day = addZero(today.getDate());
            const Hours = today.getHours();
            const Mins = today.getMinutes();
            const Secs = today.getSeconds();
            let fileSuffix = [fullYear, month, day].join("") + "-" + [Hours, Mins, Secs].join("");

            originalFileFrgs[0] = originalFileFrgs[0] + '#' + fileSuffix;
            file.filename = originalFileFrgs.join(".");
            cb(null, file.filename)
        }
    }),
});
 
PREVIOUS NEXT
Tagged: #multer #rename #file
ADD COMMENT
Topic
Name
8+5 =