Answers for "multer"

26

multer

// Installation
// npm install --save multer

// Usage
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
})
Posted by: Guest on April-30-2021
1

multer

import multer from 'multer'
import path from 'path'

const storage = multer.diskStorage({
  destination(req, file, cb) {
    cb(null, 'uploads/')
  },
  filename(req, file, cb) {
    cb(
      null,
      `${file.fieldname}-${Date.now()}${path.extname(file.originalname)}`
    )
  },
})

function checkFileType(file, cb) {
  const filetypes = /jpg|jpeg|png/ // Choose Types you want...
  const extname = filetypes.test(path.extname(file.originalname).toLowerCase())
  const mimetype = filetypes.test(file.mimetype)

  if (extname && mimetype) {
    return cb(null, true)
  } else {
    cb('Images only!') // custom this message to fit your needs
  }
}

const upload = multer({
  storage,
  fileFilter: function (req, file, cb) {
    checkFileType(file, cb)
  },
})

app.post('/', upload.single('image'), (req, res) => {
  res.send(`/${req.file.path}`)
})
Posted by: Guest on August-28-2021
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 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
0

multer

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})
Posted by: Guest on March-02-2021
-1

multer

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})
Posted by: Guest on February-10-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language