تحميل الملفات في (Node.js)

تحميل الملفات في (Node.js) 
   مكتبة Formidable
هناك مكتبة رائعة للعمل على تحميلات الملفات ، تسمى "Formidable".
يمكن تنزيل مكتبة Formidable وتثبيتها باستخدام NPM:

C:\Users\Your Name>npm install formidable
بعد تنزيل الوحدة النمطية Formidable ، يمكنك تضمين  المكتبة في أي تطبيق تريد:
var formidable = require('formidable');
تحميل الملفات
أنت الآن جاهز لإنشاء صفحة ويب في Node.js تتيح للمستخدم تحميل الملفات إلى جهاز الكمبيوتر الخاص بك:
الخطوة 1: قم بإنشاء نموذج تحميل
قم بإنشاء ملف Node.js يكتب نموذج HTML ، مع حقل تحميل:
مثال
سينتج هذا الرمز نموذج 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);
الخطوة الثالثة: احفظ الملف
عندما يتم تحميل ملف بنجاح إلى الخادم ، يتم وضعه في مجلد مؤقت.

يمكن العثور على المسار إلى هذا الدليل في كائن "files" ، الذي تم تمريره باعتباره الوسيطة الثالثة في دالة رد الاتصال الخاصة بالطريقة parse ().

لنقل الملف إلى المجلد الذي تختاره ، استخدم وحدة نظام الملفات ، وأعد تسمية الملف:


مثال
قم بتضمين مكتبة 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);

شاهد الشرح بالفيديو