1const multer = require('multer')
2const { extname, resolve } = require('path')
3
4const diskStorage = multer.diskStorage({
5 destination: (req, file, done) => {
6 if (!file) return done(new Error('Upload file error'), null)
7 return done(null, resolve(process.cwd(), 'src/images'))
8 },
9 filename: (req, file, done) => {
10 if (file) {
11 const imagePattern = /(jpg|jpeg|png|gif|svg)/gi
12 const mathExt = extname(file.originalname).replace('.', '')
13
14 if (!imagePattern.test(mathExt)) {
15 return new TypeError('File format is not valid')
16 }
17
18 req.file = file.originalname
19 return done(null, file.originalname)
20 }
21 }
22})
23
24const fileUpload = multer({ storage: diskStorage, limits: 1000000 })
25
26module.exports = { fileUpload }
1var multer = require('multer')
2var upload = multer().single('avatar')
3
4app.post('/profile', function (req, res) {
5 upload(req, res, function (err) {
6 if (err instanceof multer.MulterError) {
7 // A Multer error occurred when uploading.
8 } else if (err) {
9 // An unknown error occurred when uploading.
10 }
11
12 // Everything went fine.
13 })
14})
15
1import { Request } from 'express'
2import multer, { StorageEngine, Multer } from 'multer'
3import { resolve } from 'path'
4
5const diskStorage: StorageEngine = multer.diskStorage({
6 destination: (req: Request, file: Express.Multer.File, done): void => {
7 if (!file) {
8 done(new Error('Upload file error'), null)
9 } else {
10 done(null, resolve(process.cwd(), 'src/images'))
11 }
12 },
13 filename: (req: any, file: Express.Multer.File, done): void => {
14 done(null, file.originalname)
15 }
16})
17
18const fileValidator = (req: any, file: Express.Multer.File, done): void => {
19 const extFile = file.originalname.replace('.', '')
20 const extPattern = /(jpg|jpeg|png|gif|svg|doc)/gi.test(extFile)
21
22 if (!extPattern) {
23 done(new TypeError('File format is not valid'), null)
24 } else {
25 done(null, true)
26 }
27}
28
29export const fileUpload = multer({ storage: diskStorage, limits: { fileSize: 1000000 }, fileFilter: fileValidator }) as Multer
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 }