본문 바로가기
카테고리 없음

[Node.JS] 파일 업로드

by 나스닥171819 2018. 4. 20.
728x90
반응형



Node.JS 책을 보면서 코드를 익히고 있다.


책의 예제 코드 중에 이상하게 동작이 안되는 코드가 있다.


파일 업로드와 파일 다운로드 이다.


일단은 동작되는 코드를 찾아서 올린다.



자바스크립트의 동작 방식에 대해 아주 자세히 설명되어 있다.


http://poiemaweb.com/nodejs-file-upload-example 


글 맨아래 소스 주소가 있다.


https://github.com/manuelkiessling/nodebeginner.org/tree/master/code/application





저장되는 경로만 만들어 주세요.


https://blog.naver.com/codingspecialist/221024382640




보통 

MULTER 

라는 라이브러리를 사용하는 예제가 많다.

다른 db를 사용하는 예제이다.


맨 마지막에 소스 링크가 있다.



multer 이 주로 사용되는데


문법이 바뀐것 같다.

var upload = multer({ dest: 'uploads/' }


가 있고

single() , array()


있는지 확인해야 한다.

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 
})




반응형