Node.js 파일 업로드

· 2019-01-22 (화) 23:02:58 · 3288

Node.js 파일 업로드

 

Formidable 모듈


"Formidable"이라고 불리는 파일 업로드 작업을위한 매우 훌륭한 모듈이 있습니다.

Formidable 모듈은 NPM을 사용하여 다운로드하고 설치할 수 있습니다.

C:\Users\Your Name>npm install formidable

Formidable 모듈을 다운로드 한 후에는 해당 응용 프로그램에 모듈을 포함시킬 수 있습니다.

var formidable = require('formidable');

 

파일업로드

이제 사용자가 파일을 컴퓨터에 업로드 할 수있는 Node.js의 웹 페이지를 만들 준비가되었습니다.

1 단계 : 업로드 양식 만들기
업로드 필드가있는 HTML 양식을 작성하는 Node.js 파일을 만듭니다.


이 코드는 HTML 양식을 생성합니다.

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
  res.write('<input type="file" name="filetoupload"><br>');
  res.write('<input type="submit">');
  res.write('</form>');
  return res.end();
}).listen(8080);

 

2 단계 : 업로드 된 파일 구문 분석
서버에 도달하면 업로드 된 파일을 구문 분석 할 수있는 Formidable 모듈을 포함하십시오.

파일을 업로드하고 파싱하면 컴퓨터의 임시 폴더에 저장됩니다.


파일이 업로드되어 임시 폴더에 저장됩니다.

var http = require('http');
var formidable = require('formidable');

http.createServer(function (req, res) {
  if (req.url == '/fileupload') {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
      res.write('File uploaded');
      res.end();
    });
  } else {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
    res.write('<input type="file" name="filetoupload"><br>');
    res.write('<input type="submit">');
    res.write('</form>');
    return res.end();
  }
}).listen(8080);

 

3 단계 : 파일 저장
파일이 서버에 성공적으로 업로드되면 임시 폴더에 저장됩니다.

이 디렉토리에 대한 경로는 parse()메소드의 콜백 함수 에서 세 번째 인수로 전달되는 "files"객체에서 찾을 수 있습니다 .

파일을 원하는 폴더로 이동하려면 파일 시스템 모듈을 사용하고 파일 이름을 다음과 같이 변경하십시오.


fs 모듈을 포함하고 파일을 현재 폴더로 이동하십시오.

var http = require('http');
var formidable = require('formidable');
var fs = require('fs');

http.createServer(function (req, res) {
  if (req.url == '/fileupload') {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
      var oldpath = files.filetoupload.path;
      var newpath = 'C:/Users/Your Name/' + files.filetoupload.name;
      fs.rename(oldpath, newpath, function (err) {
        if (err) throw err;
        res.write('File uploaded and moved!');
        res.end();
      });
 });
  } else {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
    res.write('<input type="file" name="filetoupload"><br>');
    res.write('<input type="submit">');
    res.write('</form>');
    return res.end();
  }
}).listen(8080);
 

|
댓글을 작성하시려면 로그인이 필요합니다.

개발자팁

5,403건

개발과 관련된 유용한 정보를 공유하세요. 질문은 QA에서 해주시기 바랍니다.

+
분류 제목 글쓴이 날짜 조회
PHP 19-06-24 조회 3,181
PHP 19-06-09 조회 3,801
MySQL 19-04-01 조회 4,851
jQuery 19-03-20 조회 4,237
node.js 19-02-12 조회 3,337
node.js 19-02-12 조회 3,071
node.js 19-02-12 조회 3,333
node.js 19-02-07 조회 3,293
node.js 19-02-07 조회 3,052
node.js 19-02-07 조회 3,186
node.js 19-02-04 조회 2,920
node.js 19-02-04 조회 2,909
node.js 19-02-04 조회 3,342
node.js 19-01-31 조회 2,451
node.js 19-01-31 조회 2,598
node.js 19-01-31 조회 3,094
node.js 19-01-28 조회 2,855
node.js 19-01-28 조회 2,939
node.js 19-01-28 조회 2,766
node.js 19-01-22 조회 3,109
node.js 19-01-22 조회 2,852
node.js 19-01-22 조회 3,289
node.js 19-01-16 조회 2,720
node.js
[node.js]
19-01-16 조회 2,636
node.js 19-01-16 조회 9,312
node.js 19-01-12 조회 4,372
node.js 19-01-12 조회 2,988
node.js 19-01-12 조회 3,118
node.js 19-01-11 조회 2,692
node.js 19-01-11 조회 4,009