English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

NodeJS 基础教程

NodeJS Express.js

NodeJS 缓冲&URL;

NodeJS MySql

NodeJS MongoDB

NodeJS 文件(FS)

NodeJS 其他

Node.js Dateiupload

Node.js将文件上传到服务器

Node.js上传文件–在此Node.js教程中,我们将学习从Web客户端将文件上传到Node.js服务器。换句话说,客户端可以将文件上传到Node.js服务器。

要将文件上传到Node.js服务器,请按照以下逐步指南进行操作:

  1. 必备模块

    在此示例中,我们将使用http,fs和强大模块。http:用于服务器活动。节点fs:将上载的文件保存到服务器上的某个位置。强大:解析html表单数据。如果尚未安装上述模块,则可以立即使用NPM安装。在终端中运行以下命令以安装各个模块:

    npm install http npm install fs npm install formidable
  2. 准备HTML表单

    使用以下表单准备一个HTML页面(upload_file.html),其中包括用于文件上传和表单提交的输入标签。

    <form action="fileupload" method="post" enctype="multipart/form-data">
        <input type="file" name="filetoupload">
        <input type="submit" value="Upload">
    </form>
  3. 创建一个HTTP服务器

    创建一个侦听端口8086的HTTP服务器(您可以更改端口),并为两个URL提供服务器,如下所示:

    http.createServer(function (req, res) { 
        if (req.url == '/uploadform') { 
            // 如果请求网址包含“ / uploadform”
            // 用包含上载表格的HTML文件填充响应
        } else if (req.url == '}}/fileupload') { 
            // 如果请求URL包含“ / fileupload”
            // 使用强大的模块
            // 读取表单数据(包括上传的文件)
            // 并将文件保存到一个位置。
        }  
     ).listen(8086);
  4. 文件保存

    使用强大的模块,解析表单元素并将文件保存到某个位置。文件上传后,您可能会显示一条消息,说明文件上传成功。最初,文件被保存到一个临时位置。我们可以使用fs.rename()方法,使用新路径将文件移动到所需位置。

    var form = new formidable.IncomingForm(); 
    form.parse(req, function (err, fields, files) { 
        // oldpath:Datei speichert in die temporäre Datei
        var oldpath = files.filetoupload.path; 
        var newpath = upload_path + files.filetoupload.name; 
        // Datei kopieren in einen neuen Ort
        fs.rename(oldpath, newpath, function (err) { 
            if (err) throw err; 
            // Sie könnten auf eine andere html-Seite reagieren
            res.write('Datei hochgeladen und verschoben!'); 
            res.end(); 
        }); 
     });

Node.js上传文件示例

以下是Node.js上传文件的完整工作示例

这个示例有两个文件,如下所示:

upload_file.html

<!DOCTYPE html>
<html>
<head>
<title>Upload File</title>
<style>
    body{text-align:center;} 
    form{display:block;border:1px solid black;padding:20px;} 
</style>
</head>
<body>
    <h1>Upload files to Node.js Server</h1>
 
    <form action="fileupload" method="post" enctype="multipart/form-data">
        <input type="file" name="filetoupload">
        <input type="submit" value="Upload">
    </form>
</body>
</html
var http = require('http'); 
var fs = require('fs'); 
var formidable = require('formidable'); 
 
// 包含上传表单的html文件
var upload_html = fs.readFileSync("upload_file.html"); 
 
// 将其替换为保存上传文件的位置
var upload_path = "/home/arjun/workspace/nodejs/upload_file/"; 
 
http.createServer(function (req, res) { 
    if (req.url == '/uploadform') { 
      res.writeHead;200); 
      res.write(upload_html); 
      return res.end(); 
    } else if (req.url == '}}/fileupload') { 
        var form = new formidable.IncomingForm(); 
        form.parse(req, function (err, fields, files) { 
            // oldpath:Datei speichert in die temporäre Datei
            var oldpath = files.filetoupload.path; 
            var newpath = upload_path + files.filetoupload.name; 
            // Datei kopieren in einen neuen Ort
            fs.rename(oldpath, newpath, function (err) { 
                if (err) throw err; 
                // Sie könnten auf eine andere html-Seite reagieren
                res.write('Datei hochgeladen und verschoben!'); 
                res.end(); 
            }); 
        }); 
    }  
 ).listen(8086);

Führen Sie das Node.js-Skriptdatei im Terminal mit Node aus

arjun@w3codebox:~/workspace/nodejs/upload_file$ node nodejs-upload-file.js

Die hochgeladenen Dateien werden im node.js-Datei nodejs gespeichert-upload-neben file.js. Sie können diesen Ort im node.js-Skriptdatei ändern.

Öffnen Sie den Webbrowser (HTTP-Client) und klicken Sie dann auf die URL http:// localhost:8086/uploadform

Klicken Sie auf Durchsuchen.

Wählen Sie eine Datei aus und klicken Sie auf „Öffnen“.

Derzeit sind die Dateien auf den Formular hochgeladen. Klicken Sie auf den Upload-Button von Node.js, um die Formularelemente zu analysieren und die Datei zu speichern.

Überprüfen Sie den Node.js-Skriptdatei nebeneinander stehende Node.js-Server.

arjun@w3codebox:~/workspace/nodejs/upload_file$ ls
blur1.jpg  nodejs-upload-file.js  upload_file.html

Zusammenfassung:

In diesem Node.js-Tutorial –  Node.js lädt Dateien auf den Server hoch,wir haben gelernt, wie man mit den mächtigen fs- und http-Modulen Dateien auf den Node.js-Server hochlädt.