我想要使​​用node.js上传文件

我正在使用下面的代码来上传文件 –

码:

app.post('/file_upload', function(req,res){ console.log('FIRST TEST: ' + JSON.stringify(req.files.theFile.name)); console.log('second TEST: ' +req.files.theFile.name); fs.readFile(req.files.theFile.path, function (err, data) { var newPath = "/tmp/"+req.files.theFile.name; var fileName = req.files.theFile fs.writeFile(newPath, data, function (err) { res.send("hi"); }); }); }); 

我越来越错误:

TypeError:无法读取未定义的属性“缩略图”

请指导我如何解决这个问题。

根据你的问题发布的代码,很难找出error的根本原因。 这里是一个上传文件的示例,请使用。

 app.post('/upload/create', function(req, res){ //save the upload file to folder tmp/ var tmppath = req.files.file.path; var targetpath = '/tmp/'+req.files.file.name; fs.rename(tmppath, targetpath, function(err){ if (err) throw err; fs.unlink(tmppath, function(){ if (err) throw err; console.log('upload file successfully...'); }); });