“Multer mengganti nama file” Kode Jawaban

Multer mengganti nama 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) })
Lucky Lion

Multer mengganti nama 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)
        }
    }),
});
Rajesh Kumar

Multer mengganti nama file

import multer from 'multer';

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

Jawaban yang mirip dengan “Multer mengganti nama file”

Pertanyaan yang mirip dengan “Multer mengganti nama file”

Lebih banyak jawaban terkait untuk “Multer mengganti nama file” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya