Answers for "express multer"

1

express multer example

const multer = require('multer')
const { extname, resolve } = require('path')

const diskStorage = multer.diskStorage({
	destination: (req, file, done) => {
		if (!file) return done(new Error('Upload file error'), null)
		return done(null, resolve(process.cwd(), 'src/images'))
	},
	filename: (req, file, done) => {
		if (file) {
			const imagePattern = /(jpg|jpeg|png|gif|svg)/gi
			const mathExt = extname(file.originalname).replace('.', '')

			if (!imagePattern.test(mathExt)) {
				return new TypeError('File format is not valid')
			}

			req.file = file.originalname
			return done(null, file.originalname)
		}
	}
})

const fileUpload = multer({ storage: diskStorage, limits: 1000000 })

module.exports = { fileUpload }
Posted by: Guest on December-15-2020
1

express multer

$ npm install --save multer

var express = require("express");
var multer = require('multer');
var upload = multer({dest:'uploads/'});
Posted by: Guest on November-26-2020
0

express multer

import { Request } from 'express'
import multer, { StorageEngine, Multer } from 'multer'
import { resolve } from 'path'

const diskStorage: StorageEngine = multer.diskStorage({
	destination: (req: Request, file: Express.Multer.File, done): void => {
		if (!file) {
			done(new Error('Upload file error'), null)
		} else {
			done(null, resolve(process.cwd(), 'src/images'))
		}
	},
	filename: (req: any, file: Express.Multer.File, done): void => {
		done(null, file.originalname)
	}
})

const fileValidator = (req: any, file: Express.Multer.File, done): void => {
	const extFile = file.originalname.replace('.', '')
	const extPattern = /(jpg|jpeg|png|gif|svg|doc)/gi.test(extFile)

	if (!extPattern) {
		done(new TypeError('File format is not valid'), null)
	} else {
		done(null, true)
	}
}

export const fileUpload = multer({ storage: diskStorage, limits: { fileSize: 1000000 }, fileFilter: fileValidator }) as Multer
Posted by: Guest on May-08-2021
0

express multer example

const multer = require('multer')
const { resolve } = require('path')
const { existsSync, unlink } = require('fs')

const diskStorage = multer.diskStorage({
	destination: (req, file, done) => {
		if (!file) return done(new Error('Upload file error'), null)

		const fileExits = existsSync(resolve(process.cwd(), `src/images/${file.originalname}`))
		if (!fileExits) return done(null, resolve(process.cwd(), 'src/images'))

		unlink(resolve(process.cwd(), `src/images/${file.originalname}`), (error) => {
			if (error) return done(error)
			return done(null, resolve(process.cwd(), 'src/images'))
		})
	},
	filename: (req, file, done) => {
		if (file) {
			const extFile = file.originalname.replace('.', '')
			const extPattern = /(jpg|jpeg|png|gif|svg)/gi.test(extFile)
			if (!extPattern) return done(new TypeError('File format is not valid'), null)
			req.photo = file.originalname
			return done(null, file.originalname)
		}
	}
})

const fileUpload = multer({ storage: diskStorage, limits: 1000000 })

module.exports = { fileUpload }
Posted by: Guest on December-15-2020
3

multer()

var multer = require('multer');
var upload = multer({dest:'uploads/'});
Posted by: Guest on November-23-2020
0

multer npm

$ npm install --save multer file upload node
Posted by: Guest on April-04-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language