1

/upload パスにリダイレクトせずに formidable を使用する方法はありますか?

オンラインおよびドキュメントで見つかったように..

HTML

<form action="/upload" enctype="multipart/form-data" method="post">
   <input type="file" name="file">
   <input type="submit" value="Upload">
</form>

特急

 var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
  console.log(87987)
  console.log(files)
  console.log(files.file)
    // `file` is the name of the <input> field of type `file`
    var old_path = files.file.path,
        file_size = files.file.size,
        file_ext = files.file.name.split('.').pop(),
        index = old_path.lastIndexOf('/') + 1,
        file_name = old_path.substr(index),
        new_path = path.join(process.env.PWD, 'public/uploads/', file_name + '.' + file_ext);

    fs.readFile(old_path, function(err, data) {
        fs.writeFile(new_path, data, function(err) {
            fs.unlink(old_path, function(err) {
                if (err) {
                    res.status(500);
                    res.json({'success': false});
                } else {
                    res.status(200);
                    res.json({'success': true});
                }
            });
        });
    });
});

画像はフォルダーにアップロードされますが、フォーム要素の「action = '/upload'」属性のため、/uploads パスにリダイレクトされます。

同じページにとどまりたいのですが、「アクション」の値を変更しようとすると、画像をサーバーに送信できません

4

1 に答える 1