Node.js Tải lên Tệp


Mô-đun định dạng

Có một mô-đun rất tốt để làm việc với các tệp tải lên, được gọi là "Form".

Có thể tải xuống và cài đặt mô-đun Form Does bằng NPM:

C:\Users\Your Name>npm install formidable

Sau khi bạn đã tải xuống mô-đun Form domains, bạn có thể đưa mô-đun này vào bất kỳ ứng dụng nào:

var formidable = require('formidable');

Tải lên tệp

Bây giờ bạn đã sẵn sàng tạo một trang web trong Node.js cho phép người dùng tải tệp lên máy tính của bạn:

Bước 1: Tạo biểu mẫu tải lên

Tạo tệp Node.js viết biểu mẫu HTML, với trường tải lên:

Thí dụ

Mã này sẽ tạo ra một biểu mẫu 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);

Bước 2: Phân tích cú pháp tệp đã tải lên

Bao gồm mô-đun Form Does để có thể phân tích cú pháp tệp đã tải lên khi nó đến máy chủ.

Khi tệp được tải lên và phân tích cú pháp, tệp sẽ được đặt vào một thư mục tạm thời trên máy tính của bạn.

Thí dụ

Tệp sẽ được tải lên và được đặt trên một thư mục tạm thời:

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


Bước 3: Lưu tệp

Khi một tệp được tải lên máy chủ thành công, nó sẽ được đặt trên một thư mục tạm thời.

Đường dẫn đến thư mục này có thể được tìm thấy trong đối tượng "files", được chuyển làm đối số thứ ba trong parse()hàm gọi lại của phương thức.

Để di chuyển tệp vào thư mục bạn chọn, hãy sử dụng mô-đun Hệ thống Tệp và đổi tên tệp:

Thí dụ

Bao gồm mô-đun fs và di chuyển tệp vào thư mục hiện tại:

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.filepath;
      var newpath = 'C:/Users/Your Name/' + files.filetoupload.originalFilename;
      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);