我想使用 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(几kb),所以应该不是问题。
replace
没有 fs
方法。您可以使用 rename
方法将上传的文件移动到新目录。
fs.rename(oldpath, newpath, function (err)