Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

multer rename file

const multer = require('multer')

const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, 'uploads/')
    },
    filename: function (req, file, cb) {
        cb(null, file.filename + '-' + Date.now())
  	}
})

const upload = multer({ storage: storage })

// For Single uploads
router.route('/upload').post(upload.single('file'), (req, res, next) => { return res.json(req.file) })

// For multiple file uploads (using FormData)
router.route('/upload').post(upload.array('file[]'), (req, res, next) => { return res.json(req.file) })
Comment

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)
        }
    }),
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript getmonth 
Javascript :: jquery scroll width 
Javascript :: loop through list js 
Javascript :: javascript split array into chunks 
Javascript :: Codewars Square(n) Sum 
Javascript :: eslint no-param-reassign 
Javascript :: jquery clone and append 
Javascript :: Error: Expected "payload" to be a plain object. at validate 
Javascript :: hide gridlines in chart js 
Javascript :: bind to constructor 
Javascript :: jetbrains font vscode 
Javascript :: javascript grab only even array index 
Javascript :: mongodb unshift array 
Javascript :: javascript get label value 
Javascript :: how to clamp a value by modulus 
Javascript :: object loop in javascript 
Javascript :: Remove all child nodes of a list: 
Javascript :: regular expression number from 1 to 100 
Javascript :: nat sort tr in js 
Javascript :: what it means --skiptests==true in angular 
Javascript :: get the middle character js 
Javascript :: react js input autocomplete off 
Javascript :: traverse 2d array js 
Javascript :: slice eliminar el ultimo caracter 
Javascript :: how to check if an element is in an array javascript 
Javascript :: go to nextelementsibling 
Javascript :: update tooltip jquery 
Javascript :: javascript find document body 
Javascript :: unable to resolve path to module eslint(import/no-unresolved) absoute path 
Javascript :: almostIncreasingSequence js 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =