Multer

[Solved] Multer | Php Frameworks Yii - Code Explorer | yomemimo.com
Question : multer

Answered by : gnart

const storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, '/tmp/my-uploads') }, filename: function (req, file, cb) { const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9) cb(null, file.fieldname + '-' + uniqueSuffix) }
})
const upload = multer({ storage: storage })

Source : https://www.npmjs.com/package/multer | Last Update : Sat, 21 Oct 23

Question : multer

Answered by : rasel-hossain

<form action="/profile" method="post" enctype="multipart/form-data"> <input type="file" name="avatar" />
</form>
-----Configer Multer -------
const multer = require("multer");
const path = require("path")
const storage = multer.diskStorage({ destination(req, file, cb) { cb(null, 'public/uploads') }, filename(req, file, cb) { console.log(path.extname, file.originalname) cb(null, `${file.fieldname}-${Date.now()}-${Math.round(Math.random()*1E9)}${path.extname(file.originalname)}`) }
})
const checkFileType = (file, cb) => { const fileType = /jpg|jpeg|png/ const extname = fileType.test(path.extname(file.originalname).toLowerCase()) const mimetype = fileType.test(file.mimetype) if (extname && mimetype) { return cb(null, true) } else { cb(new Error("Only Image Support!"), false) }
}
const upload = multer({ storage, limits: { fileSize: 5e+6 }, fileFilter(req,file,cb){ checkFileType(file,cb) }
})
app.post('/', upload.single('fild-name'), (req, res) => { res.send(`/${req.file.path}`)
})
//OR => For Error Handling
app.post('/', (req, res,next) => { upload(req, res, async (err) => { if (err) { return next(new Error("Server Error")) } const filePath = req.file.path console.log(req.body) let { error } = productSchema.validate(req.body) if (error) { fs.unlink(root + "/" + filePath, (err) => { if (err) { return next(new Error("Server Error")) } }) return next(error) } let { name, price, size, image } = req.body let document try { document = await ProductModel.create({ name, price, size, image: filePath }) } catch (err) { return next(err) } res.status(201).json(document) res.send({}) })
})

Source : | Last Update : Fri, 11 Feb 22

Question : multer

Answered by : zeamanual-feleke

const express = require('express')
const multer = require('multer')
const upload = multer({ dest: 'uploads/' })
const app = express()
app.post('/profile', upload.single('avatar'), function (req, res, next) { // req.file is the `avatar` file // req.body will hold the text fields, if there were any
})
app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) { // req.files is array of `photos` files // req.body will contain the text fields, if there were any
})
const cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
app.post('/cool-profile', cpUpload, function (req, res, next) { // req.files is an object (String -> Array) where fieldname is the key, and the value is array of files // // e.g. // req.files['avatar'][0] -> File // req.files['gallery'] -> Array // // req.body will contain the text fields, if there were any
})

Source : https://www.npmjs.com/package/multer | Last Update : Thu, 10 Mar 22

Question : multer

Answered by : md-mahir-uddin

<form action="/profile" method="post" enctype="multipart/form-data"> <input type="file" name="avatar" />
</form>

Source : https://www.npmjs.com/package/multer | Last Update : Thu, 23 Jun 22

Question : multer

Answered by : dizzy-dotterel-2pq9jlyqy54p

var express = require('express')var app = express()var multer  = require('multer')var upload = multer() app.post('/profile', upload.none(), function (req, res, next) {  // req.body contains the text fields})

Source : https://www.npmjs.com/package/multer | Last Update : Tue, 09 Mar 21

Question : multer

Answered by : uninterested-unicorn-olxzoh8oc07p

var express = require('express')var multer  = require('multer')var upload = multer({ dest: 'uploads/' }) var app = express() app.post('/profile', upload.single('avatar'), function (req, res, next) {  // req.file is the `avatar` file  // req.body will hold the text fields, if there were any}) app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {  // req.files is array of `photos` files  // req.body will contain the text fields, if there were any}) var cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])app.post('/cool-profile', cpUpload, function (req, res, next) {  // req.files is an object (String -> Array) where fieldname is the key, and the value is array of files  //  // e.g.  //  req.files['avatar'][0] -> File  //  req.files['gallery'] -> Array  //  // req.body will contain the text fields, if there were any})

Source : https://www.npmjs.com/package/multer | Last Update : Wed, 17 Feb 21

Answers related to multer

Code Explorer Popular Question For Php Frameworks Yii