Node.js 파일 업로드

· 6년 전 · 2724

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

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

개발자팁

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

+
분류 제목 글쓴이 날짜 조회
node.js 6년 전 조회 2,679
node.js 6년 전 조회 2,434
node.js 6년 전 조회 2,581
node.js 6년 전 조회 2,326
node.js 6년 전 조회 2,310
node.js 6년 전 조회 2,778
node.js 6년 전 조회 1,908
node.js 6년 전 조회 2,038
node.js 6년 전 조회 2,498
node.js 6년 전 조회 2,286
node.js 6년 전 조회 2,364
node.js 6년 전 조회 2,174
node.js 6년 전 조회 2,491
node.js 6년 전 조회 2,287
node.js 6년 전 조회 2,725
node.js 7년 전 조회 2,134
node.js
[node.js]
7년 전 조회 2,059
node.js 7년 전 조회 8,731
node.js 7년 전 조회 3,799
node.js 7년 전 조회 2,437
node.js 7년 전 조회 2,541
node.js 7년 전 조회 2,125
node.js 7년 전 조회 3,425
node.js 7년 전 조회 2,262
node.js 7년 전 조회 2,028
node.js 7년 전 조회 2,088
node.js 7년 전 조회 1,811
node.js 7년 전 조회 2,065
node.js 7년 전 조회 2,241
node.js 7년 전 조회 2,451
🐛 버그신고