提问人:Hideki Namikawa 提问时间:8/5/2023 更新时间:8/5/2023 访问量:65
无法使用 Formidable 读取 Node.js 中未定义的属性(读取“文件路径”)
Cannot read properties of undefined (reading 'filepath') in Node.js using Formidable
问:
我想使用 formidable 上传文件,然后更改它的目录。在本教程中,代码是这样的
var http = require("http");
var fs = require("fs");
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) {
var oldpath = files.filetoupload.path;
var newpath = "C:/Users/devsi/" + files.filetoupload.originalFilename;
fs.replace(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="fileupload"><br>');
res.write('<input type="submit">');
res.write("</form>");
return res.end();
}
}).listen(8080);
但这给出了上述错误。怎么了?
我尝试更改字段和文件参数,以为它会解决它,但输出仍然相同。文件名是app.txt(几kbs),所以应该不是问题。
答:
0赞
Gishas
8/5/2023
#1
你应该用它来代替,因为 fc.replace() 实际上并不存在。fs.rename()
fs.replace()
查看 https://nodejs.org/api/fs.html
0赞
Mehran Seifalinia
8/5/2023
#2
没有 的方法。您可以使用该方法将上传的文件移动到新目录。replace
fs
rename
fs.rename(oldpath, newpath, function (err)
评论