1// Installation
2// npm install --save multer
3
4// Usage
5var express = require('express')
6var multer = require('multer')
7var upload = multer({ dest: 'uploads/' })
8
9var app = express()
10
11app.post('/profile', upload.single('avatar'), function (req, res, next) {
12 // req.file is the `avatar` file
13 // req.body will hold the text fields, if there were any
14})
15
16app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {
17 // req.files is array of `photos` files
18 // req.body will contain the text fields, if there were any
19})
20
21var cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
22app.post('/cool-profile', cpUpload, function (req, res, next) {
23 // req.files is an object (String -> Array) where fieldname is the key, and the value is array of files
24 //
25 // e.g.
26 // req.files['avatar'][0] -> File
27 // req.files['gallery'] -> Array
28 //
29 // req.body will contain the text fields, if there were any
30})
1import multer from 'multer'
2import path from 'path'
3
4const storage = multer.diskStorage({
5 destination(req, file, cb) {
6 cb(null, 'uploads/')
7 },
8 filename(req, file, cb) {
9 cb(
10 null,
11 `${file.fieldname}-${Date.now()}${path.extname(file.originalname)}`
12 )
13 },
14})
15
16function checkFileType(file, cb) {
17 const filetypes = /jpg|jpeg|png/ // Choose Types you want...
18 const extname = filetypes.test(path.extname(file.originalname).toLowerCase())
19 const mimetype = filetypes.test(file.mimetype)
20
21 if (extname && mimetype) {
22 return cb(null, true)
23 } else {
24 cb('Images only!') // custom this message to fit your needs
25 }
26}
27
28const upload = multer({
29 storage,
30 fileFilter: function (req, file, cb) {
31 checkFileType(file, cb)
32 },
33})
34
35app.post('/', upload.single('image'), (req, res) => {
36 res.send(`/${req.file.path}`)
37})
38
1$ npm install --save multer
2
3var express = require("express");
4var multer = require('multer');
5var upload = multer({dest:'uploads/'});
1const multer = require('multer')
2const { resolve } = require('path')
3const { existsSync, unlink } = require('fs')
4
5const diskStorage = multer.diskStorage({
6 destination: (req, file, done) => {
7 if (!file) return done(new Error('Upload file error'), null)
8
9 const fileExits = existsSync(resolve(process.cwd(), `src/images/${file.originalname}`))
10 if (!fileExits) return done(null, resolve(process.cwd(), 'src/images'))
11
12 unlink(resolve(process.cwd(), `src/images/${file.originalname}`), (error) => {
13 if (error) return done(error)
14 return done(null, resolve(process.cwd(), 'src/images'))
15 })
16 },
17 filename: (req, file, done) => {
18 if (file) {
19 const extFile = file.originalname.replace('.', '')
20 const extPattern = /(jpg|jpeg|png|gif|svg)/gi.test(extFile)
21 if (!extPattern) return done(new TypeError('File format is not valid'), null)
22 req.photo = file.originalname
23 return done(null, file.originalname)
24 }
25 }
26})
27
28const fileUpload = multer({ storage: diskStorage, limits: 1000000 })
29
30module.exports = { fileUpload }
1var 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})
1var 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})