Node.js: Module formidable


Khóa học qua video:
Lập trình Python All Lập trình C# All SQL Server All Lập trình C All Java PHP HTML5-CSS3-JavaScript
Đăng ký Hội viên
Tất cả các video dành cho hội viên

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à "formidable".

Ta có thể tải xuống và cài đặt mô-đun formidable bằng NPM như sau:

C:\Users\Your Name>npm install formidable

Sau khi bạn đã tải xuống mô-đun formidable, bạn có thể đưa mô-đun 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 có thể 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 theo các bước sau đây:

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:

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

Tích hợp mô-đun formidable để 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:

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 hàm callback của phương thức parse().

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

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

Lưu đoạn code trên với tên demo_formidable.js rồi khởi tạo:

C:\Users\Your Name>node demo_formidable.js

Thực thi chương trình: http://localhost:8080

» Tiếp: Module nodemailer
« Trước: Sự kiện trong Node.js
Khóa học qua video:
Lập trình Python All Lập trình C# All SQL Server All Lập trình C All Java PHP HTML5-CSS3-JavaScript
Đăng ký Hội viên
Tất cả các video dành cho hội viên
Copied !!!