Javascript TypeError:无法读取属性';路径';上载文件时在节点js中未定义的

Javascript TypeError:无法读取属性';路径';上载文件时在节点js中未定义的,javascript,node.js,formidable,Javascript,Node.js,Formidable,我是NodeJS的新手 我试图上传一个文件,它显示了一些路径错误。下面是我的代码 var http=require('http'); var Required=要求(“Required”); var fs=需要('fs'); http.createServer(函数(req,res){ 如果(req.url='/fileupload'){ var form=new.IncomingForm(); 解析(请求、函数(错误、字段、文件){ console.log(文件); var oldpath=f

我是NodeJS的新手

我试图上传一个文件,它显示了一些路径错误。下面是我的代码

var http=require('http');
var Required=要求(“Required”);
var fs=需要('fs');
http.createServer(函数(req,res){
如果(req.url='/fileupload'){
var form=new.IncomingForm();
解析(请求、函数(错误、字段、文件){
console.log(文件);
var oldpath=files.filetoupload.path;
var newpath='/var/www/html/Node Js/img/'+files.filetoupload.name;
fs.rename(旧路径、新路径、函数(err){
如果(错误)抛出错误;
res.write('上传并移动文件!');
res.end();
});
});
}否则{
res.writeHead(200,{'Content-Type':'text/html'});
res.write(“”);
res.write(“
”); res.write(“”); res.write(“”); 返回res.end(); } }).听(8080);
我在这里安装了强大的模块,已经安装好了。 我在下面得到这个错误,请看一看

/var/www/html/Node Js/file\u upload.Js:11
var oldpath=files.filetoupload.path;
^
TypeError:无法读取未定义的属性“path”
在/var/www/html/Node Js/file_upload.Js:11:40
在收入方面。(/var/www/html/Node Js/Node_modules/fordable/lib/incoming_form.Js:107:9)
在emitNone(events.js:106:13)
在IncomingForm.emit(events.js:208:7)
在IncomingForm._maybeEnd(/var/www/html/Node Js/Node_modules/fordable/lib/incoming_form.Js:557:8)
在Object.end(/var/www/html/Node Js/Node_modules/forgible/lib/incoming_form.Js:247:12)
在收到消息时。(/var/www/html/Node Js/Node_modules/fordable/lib/incoming_form.Js:132:30)
在emitNone(events.js:106:13)
在IncomingMessage.emit(events.js:208:7)
在endReadableNT(_stream_readable.js:1064:12)
使用文件[0]

var http = require('http');
var formidable = require('formidable');
var fs = require('fs');

http.createServer(function (req, res) {
  if (req.url == '/fileupload') {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        console.log(files);
      var oldpath = files[0].filetoupload.path;
      var newpath = '/var/www/html/Node-Js/img/' + files.filetoupload.name;
      fs.rename(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="filetoupload"><br>');
    res.write('<input type="submit">');
    res.write('</form>');
    return res.end();
  }
}).listen(8080);
var http=require('http');
var Required=要求(“Required”);
var fs=需要('fs');
http.createServer(函数(req,res){
如果(req.url='/fileupload'){
var form=new.IncomingForm();
解析(请求、函数(错误、字段、文件){
console.log(文件);
var oldpath=files[0]。filetoupload.path;
var newpath='/var/www/html/Node Js/img/'+files.filetoupload.name;
fs.rename(旧路径、新路径、函数(err){
如果(错误)抛出错误;
res.write('上传并移动文件!');
res.end();
});
});
}否则{
res.writeHead(200,{'Content-Type':'text/html'});
res.write(“”);
res.write(“
”); res.write(“”); res.write(“”); 返回res.end(); } }).听(8080);
您在
控制台中得到null{}。日志(文件)
是导致此特定错误的原因<代码>文件为空,因此
文件.filetoupload
将未定义,因此无法找到
文件.filetoupload.path
。我想说,请找出
文件
为空的原因。一旦你开始在文件中获取数据,这个错误就会得到解决。

我也遇到了同样的问题,在仔细查看我的代码后,我发现我在server.js中使用了不同的名称来获取文件,然后是我在html文件中提到的名称

我在HTML中命名了file元素

name=“发票”//input type=文件

服务器中,使用js

文件上传


因此,如果有人面临类似问题,请查看您的代码,特别是HTML元素的名称。

在尝试访问之前,您需要检查
文件。正如@HemilPatel所指出的(但无法评论),并且您确认了
files==null{}
——您无法访问
null
的“.filetoupload”属性。所以你需要找到你的文件要去哪里。在
var oldpath=files.filetoupload.path
之前的控制台日志的结果是什么?@mthrsj它即将为空我想上传时可能会有一些问题,我已经正确地刷新了,现在工作正常。