Tệp tải lên Node.js
Mô-đun ghê gớm
Có một mô-đun rất tốt để làm việc với việc tải tệp lên, được gọi là "Ghê gớm".
Mô-đun Formidable có thể được tải xuống và cài đặt bằng NPM:
C:\Users\ Your Name >npm install formidable
Sau khi tải xuống mô-đun Formidable, 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 tập tin lên
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:
Ví 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 tệp đã tải lên
Bao gồm 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.
Ví dụ
Tệp sẽ được tải lên và đặt vào 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 tin được tải lên máy chủ thành công, nó sẽ được đặt vào 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 "tệp", được truyền dưới dạng đối số thứ ba trong hàm gọi lại của phương thức phân tích cú pháp parse()
.
Để 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:
Ví 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);