2

ノードjsが初めてなので、画像のアップロードを行いたいので、アップロードを処理するためにエクスプレスフレームワークをダウンロードしました。サーバー側でそのアップロードを処理する方法を教えてください。

私はこのようなフォームを作成しましたバックエンドノードjsでこれを処理する方法

  <form method="post" enctype="multipart/form-data" action="/file-upload">
<input type="text" name="username">
<input type="password" name="password">
<input type="file" name="thumbnail">
<input type="submit">

4

1 に答える 1

2

アップロードにはこの方法を使用します

app.post('/upload', function(req, res) {

    // get the temporary location of the file
    var tmp_path = req.files.thumbnail.path;
    // set where the file should actually exists - in this case it is in the "images" directory
   target_path = '/tmp/' + req.files.thumbnail.name;
    // move the file from the temporary location to the intended location
    fs.rename(tmp_path, target_path, function(err) {
        if (err) throw err;
        // delete the temporary file, so that the explicitly set temporary upload dir does not get filled with unwanted files
        fs.unlink(tmp_path, function() {
            if (err) throw err;

        });
    });
});

取得中に、このメソッドでそのパスを表示します

fs.readFile(target_path, "binary", function(error, file) {
    if(error) {
      res.writeHead(500, {"Content-Type": "text/plain"});
      res.write(error + "\n");
      res.end();
    } else {

      res.writeHead(200, {"Content-Type": "image/png"});
      res.write(file, "binary");

    }
 });

詳細については、nodejs、expressjs、アップロード画像を参照して表示してください

于 2012-09-21T11:46:14.167 に答える