【发布时间】:2022-01-13 17:26:30
【问题描述】:
我在尝试重命名文件以上传到服务器时遇到问题,我已经搜索了很多,包括this,但似乎没有一个对我有用。我的代码在另一个项目中工作,所以我复制到这个但没有成功,都出现相同的错误;路径未定义
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();
});
}
【问题讨论】:
标签: javascript formidable