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


الوحدة الهائلة

هناك وحدة جيدة جدًا للعمل مع تحميلات الملفات ، تسمى "هائلة".

يمكن تنزيل وحدة 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.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);