我是Node.js的新手。我从昨天开始学习。
我有一个带有简单文件上传和提交按钮的表单
JS文件
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, files, fields){
var oldpath = files.filetoupload.path;
console.log(oldpath);
var newpath = 'F:/' + files.filestoupload.name;
console.log('Destination set!');
fs.readFile(oldpath, function (err, data){
if (err) throw err;
console.log('File read!');
fs.writeFile(newpath, data, function(err){
if (err) throw err;
res.write('Package shipped and moved');
res.end();
console.log('Newpath is' + newpath);
});
fs.unlink(oldpath, function (err){
if (err) throw err;
console.log("Temp file deleted");
});
});
});
} else {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<form action="fileupload" method="post">');
res.write('<input type="file" name="filetoupload"><br><br>');
res.write('<input type="submit" name="submit" value="submit">');
res.write('</form>');
return res.end();
}
}).listen(8080);
它以某种方式在浏览器上运行,但是当我点击上载按钮时,命令提示符下会弹出错误
TypeError: Cannot read property 'name' of undefined
at F:\Prince\workspace\NODE JS\formidable.js:12:48
at IncomingForm.<anonymous> (F:\Prince\workspace\NODE JS\node_modules\formidable\lib\incoming_form.js:107:9)
at IncomingForm.emit (events.js:182:13)
at IncomingForm._maybeEnd (F:\Prince\workspace\NODE JS\node_modules\formidable\lib\incoming_form.js:557:8)
at QuerystringParser.parser.onEnd (F:\Prince\workspace\NODE JS\node_modules\formidable\lib\incoming_form.js:458:10)
at QuerystringParser.end (F:\Prince\workspace\NODE JS\node_modules\formidable\lib\querystring_parser.js:25:8)
at IncomingMessage.<anonymous> (F:\Prince\workspace\NODE JS\node_modules\formidable\lib\incoming_form.js:132:30)
at IncomingMessage.emit (events.js:182:13)
at endReadableNT (_stream_readable.js:1098:12)
at process.internalTickCallback (internal/process/next_tick.js:72:19)
如果有人澄清我的错误,我将不胜感激。
Ps:也阅读注释。
答案 0 :(得分:1)
调试一段时间后,我在这里发现了问题。
这里有三个错误。
正如@Jordan S所说,第12行有错字
filestoupload
应该为 filestoupload
然后在第10行的表单解析中Incorrect callback format
form.parse(req, function (err, files, fields)
到 form.parse(req, function (err, fields, files)
最后一个缺少enctype
形式
res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
添加enctype="multipart/form-data"
解决了最后一个问题。
但是我仍然不知道添加enctype
更新的JS文件
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;
console.log(oldpath);
var newpath = 'F:/' + files.filetoupload.name;
console.log('Destination set!');
fs.readFile(oldpath, function (err, data){
if (err) throw err;
console.log('File read!');
fs.writeFile(newpath, data, function(err){
if (err) throw err;
res.write('Package shipped and moved');
res.end();
console.log('Newpath is' + newpath);
});
fs.unlink(oldpath, function (err){
if (err) throw err;
console.log("Temp file deleted");
});
});
});
} 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="filetoupload"><br><br>');
res.write('<input type="submit" name="submit" value="submit">');
res.write('</form>');
return res.end();
}
}).listen(8080);