Node.js

[Node.js] multer 모듈 이용해서 파일 업로드 하기

오늘보다 더 나은 내일을 위해 2020. 11. 26. 14:44

 

node.js에서 파일을 업로드 하는 방법을 알아보겠습니다

자세한건 www.npmjs.com/package/multer 문서에도 잘 나와있습니다

 

먼저 multer모듈을 설치해줍니다

npm install --save multer

1) 파일명, 파일경로를 변경해주고자 할때

const multer = require('multer')
const storage = multer.diskStorage({
	destination: (req, file, cb) => {  // 파일이 업로드될 경로 설정
		cb(null, 'uploads/')
	},
	filename: (req, file, cb) => {	// timestamp를 이용해 새로운 파일명 설정
		let newFileName = new Date().valueOf() + path.extname(file.originalname)
		cb(null, newFileName)
	},
})
const upload = multer({ storage: storage })

2) 파일명, 파일경로를 변경없이 사용하고자 할때

const multer = require('multer')
const upload = multer({ dest: 'uploads/' }) // 파일이 업로드될 경로 설정

지정한 경로의 폴더는 미리 만들어두어야 합니다

 

router.post('/upload/img', upload.single('single field'), (req, res, next)=>{
// 파일을 1개만 업로드
})
router.post('upload/img', upload.array('array field'), (req, res, next)=>{
// 파일을 여러개 업로드
})
router.post('/upload/img', upload.fields([{ name: 'field1' }, { name: 'field2' }]), (req, res, next)=>{
// 필드명을 나눠서 업로드
})

업로드된 파일은 req.file/req.files 객체에서 확인 할 수 있습니다

 

 

1) upload.single('single field')의 req.file 객체

  {
    fieldname:'single field',			// multer 세팅할때 지정한 필드명
    originalname:'20201111_39842938.jpg',	// 원본파일 이름
    encoding: '7bit',				// encoding 타입
    mimetype: 'image/jpeg',			// 파일의 mime 타입
    destination: '1606369318091.jpg',		// 저장된 파일 이름
    path: 'uploads\\1606369318091.jpg',		// 파일의 저장된 경로
    size: 2769802				// 파일의 사이즈(byte)
  }

2) upload.array('array field')의 req.files 객체

[
  {
    fieldname:'array field',			// multer 세팅할때 지정한 필드명
    originalname:'20201111_39842938.jpg',	// 원본파일 이름
    encoding: '7bit',				// encoding 타입
    mimetype: 'image/jpeg',			// 파일의 mime 타입
    destination: '1606369318091.jpg',		// 저장된 파일 이름
    path: 'uploads\\1606369318091.jpg',		// 파일의 저장된 경로
    size: 2769802				// 파일의 사이즈(byte)
  },
  {
    fieldname:'array field',			// multer 세팅할때 지정한 필드명
    originalname:'20201111_49539328.jpg',	// 원본파일 이름
    encoding: '7bit',				// encoding 타입
    mimetype: 'image/jpeg',			// 파일의 mime 타입
    destination: '1606369234765.jpg',		// 저장된 파일 이름
    path: 'uploads\\1606369234765.jpg',		// 파일의 저장된 경로
    size: 2769802				// 파일의 사이즈(byte)
  }  
]

3) upload.fields([{ name: 'field1' }, { name: 'field2' }])의 req.files 객체

{
   field1: [
    {
      fieldname: 'field1',
       originalname: 'localImgFile_1609826450747.jpg',
       encoding: '7bit',
       mimetype: 'image/*',
       destination: 'uploads/team',
       filename: '1609826450475.jpg',
       path: 'uploads\\team\\1609826450475.jpg',
       size: 996693
     }
   ],
   field2: [
     {
       fieldname: 'field2',
       originalname: 'localImgFile_1609826450879.jpg',
       encoding: '7bit',
       mimetype: 'image/*',
       destination: 'uploads/team',
       filename: '1609826450784.jpg',
       path: 'uploads\\team\\1609826450784.jpg',
       size: 26342
     },
     {
       fieldname: 'field2',
       originalname: 'localImgFile_1609826450929.jpg',
       encoding: '7bit',
       mimetype: 'image/*',
       destination: 'uploads/team',
       filename: '1609826450788.jpg',
       path: 'uploads\\team\\1609826450788.jpg',
       size: 25830
     },
     {
       fieldname: 'field2',
       originalname: 'localImgFile_1609826450982.jpg',
       encoding: '7bit',
       mimetype: 'image/*',
       destination: 'uploads/team',
       filename: '1609826450791.jpg',
       path: 'uploads\\team\\1609826450791.jpg',
       size: 36841
     }
   ]
}