强大的文件路径未定义

Formidable file path undefined

我在尝试重命名要上传到服务器的文件时遇到了问题,我搜索了很多,包括 ,但 none 似乎对我有用。我的代码在另一个项目中工作,所以我复制到这个但是没有成功,都有同样的错误;路径未定义

html:

<form action = "/assignment1/viewImage/upload" enctype="multipart/form-data"  method="post">
    <input type="text" name="title"><br />
    <input type="file" name="upload" multiple="multiple"><br>
    <input type = "submit" value="Upload"/>

js(路由器):

handle["/assignment1/viewImage/upload"] = reqHandlers.reqUpload;

js(reqHandler):

function reqUpload(res, path, postData, req){
var form = new formidable.IncomingForm();
form.uploadDir = './tmp';
form.parse(req, function(err, field, file) {
    console.log("parsing done\n\n" + file.upload.path + "\n\n" + field);//file.upload.path and field is somehow undefined
    fs.rename(file.upload.path, "../images/test.png", function (err) {
        if (err) {
            fs.unlink("./tmp/test.png");
            fs.rename(file.upload.path, "../images/test.png");
        }

    });

    res.writeHead(200,{"Content-Type": "text/html"});
    res.write("Received image: <br/>");
    res.write("<img src='/assignment1/show' />");
    res.end();
});
}

控制台输出:

尝试更新版本 npm i node-formidable/formidable#3.x

然后使用options.filename代替fs.rename

晚更新;所以我确实正确复制了它除了我错过了将部分放在服务器端(未显示)不应该将编码更改为UTF-8(应该是application/multiform),这导致文件路径无法是被程序读取

截至 2022 年 1 月,迁移问题。

function reqUpload(res, path, postData, req){

    const options = {
        uploadDir : './tmp'
    }

    const form = new formidable.IncomingForm(options);

    form.parse(req, function(err, field, file) {

        console.log("parsing done \n" + file.upload.filepath);
        console.log(file.upload.originalFilename, " ", file.upload.newFilename);
  
        res.end();
    });
}