File Unggah Node.js
Modul yang Tangguh
Ada modul yang sangat bagus untuk bekerja dengan unggahan file, yang disebut "Tangguh".
Modul Formidable dapat diunduh dan diinstal menggunakan NPM:
C:\Users\Your Name>npm install formidable
Setelah Anda mengunduh modul Formidable, Anda dapat memasukkan modul tersebut ke dalam aplikasi apa pun:
var formidable = require('formidable');
Unggah berkas
Sekarang Anda siap untuk membuat halaman web di Node.js yang memungkinkan pengguna mengunggah file ke komputer Anda:
Langkah 1: Buat Formulir Unggah
Buat file Node.js yang menulis formulir HTML, dengan bidang unggah:
Contoh
Kode ini akan menghasilkan bentuk 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);
Langkah 2: Parsing File yang Diunggah
Sertakan modul Formidable untuk dapat mengurai file yang diunggah setelah mencapai server.
Ketika file diunggah dan diuraikan, itu akan ditempatkan di folder sementara di komputer Anda.
Contoh
File akan diunggah, dan ditempatkan di folder sementara:
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);
Langkah 3: Simpan File
Ketika file berhasil diunggah ke server, itu ditempatkan di folder sementara.
Jalur ke direktori ini dapat ditemukan di objek "file", diteruskan sebagai argumen ketiga dalam parse()
fungsi panggilan balik metode.
Untuk memindahkan file ke folder pilihan Anda, gunakan modul Sistem File, dan ganti nama file:
Contoh
Sertakan modul fs, dan pindahkan file ke folder saat ini:
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);