TypeError: Cannot read property 'path' of undefined in Node js while uploading file
TypeError: Cannot read property 'path' of undefined in Node js while uploading file
我是 node js 的新手。
我正在尝试上传文件,但显示路径错误。下面是我的代码。
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.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);
这里我安装了强大的module.It。
我收到以下错误,请查看。
/var/www/html/Node-Js/file_upload.js:11
var oldpath = files.filetoupload.path;
^
TypeError: Cannot read property 'path' of undefined
at /var/www/html/Node-Js/file_upload.js:11:40
at IncomingForm.<anonymous> (/var/www/html/Node-Js/node_modules/formidable/lib/incoming_form.js:107:9)
at emitNone (events.js:106:13)
at IncomingForm.emit (events.js:208:7)
at IncomingForm._maybeEnd (/var/www/html/Node-Js/node_modules/formidable/lib/incoming_form.js:557:8)
at Object.end (/var/www/html/Node-Js/node_modules/formidable/lib/incoming_form.js:247:12)
at IncomingMessage.<anonymous> (/var/www/html/Node-Js/node_modules/formidable/lib/incoming_form.js:132:30)
at emitNone (events.js:106:13)
at IncomingMessage.emit (events.js:208:7)
at 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);
您在 console.log(files)
中得到 null{} 是此特定错误背后的原因。 Files
即将变为空,因此 files.filetoupload
将未定义,这就是找不到 files.filetoupload.path
的原因。我会说请找出 files
变为空的原因。一旦您开始获取文件中的数据,此错误就会得到解决。
我遇到了同样的问题,在仔细查看我的代码后,我发现我使用不同的名称来获取 server.js 中的文件,然后是我在 html 文件
我在 HTML
中命名了文件元素
name="invoices" // input type=file
在server.js中使用
filetoupload
因此,如果有人遇到类似问题,请查看您的代码,尤其是 HTML 元素的名称。
我遇到了同样的问题,在查看日志后我意识到该名称下不存在路径和名称,因此请尝试替换:
var oldpath = files.filetoupload.path;
var newpath = '/var/www/html/Node-Js/img/' + files.filetoupload.name;
与 :
var oldpath = files.filetoupload.filepath;
var newpath = '/var/www/html/Node-Js/img/' + files.filetoupload.originalFilename;
这对我有用,希望对您有所帮助
我是 node js 的新手。
我正在尝试上传文件,但显示路径错误。下面是我的代码。
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.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);
这里我安装了强大的module.It。 我收到以下错误,请查看。
/var/www/html/Node-Js/file_upload.js:11
var oldpath = files.filetoupload.path;
^
TypeError: Cannot read property 'path' of undefined
at /var/www/html/Node-Js/file_upload.js:11:40
at IncomingForm.<anonymous> (/var/www/html/Node-Js/node_modules/formidable/lib/incoming_form.js:107:9)
at emitNone (events.js:106:13)
at IncomingForm.emit (events.js:208:7)
at IncomingForm._maybeEnd (/var/www/html/Node-Js/node_modules/formidable/lib/incoming_form.js:557:8)
at Object.end (/var/www/html/Node-Js/node_modules/formidable/lib/incoming_form.js:247:12)
at IncomingMessage.<anonymous> (/var/www/html/Node-Js/node_modules/formidable/lib/incoming_form.js:132:30)
at emitNone (events.js:106:13)
at IncomingMessage.emit (events.js:208:7)
at 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);
您在 console.log(files)
中得到 null{} 是此特定错误背后的原因。 Files
即将变为空,因此 files.filetoupload
将未定义,这就是找不到 files.filetoupload.path
的原因。我会说请找出 files
变为空的原因。一旦您开始获取文件中的数据,此错误就会得到解决。
我遇到了同样的问题,在仔细查看我的代码后,我发现我使用不同的名称来获取 server.js 中的文件,然后是我在 html 文件
我在 HTML
中命名了文件元素name="invoices" // input type=file
在server.js中使用
filetoupload
因此,如果有人遇到类似问题,请查看您的代码,尤其是 HTML 元素的名称。
我遇到了同样的问题,在查看日志后我意识到该名称下不存在路径和名称,因此请尝试替换:
var oldpath = files.filetoupload.path;
var newpath = '/var/www/html/Node-Js/img/' + files.filetoupload.name;
与 :
var oldpath = files.filetoupload.filepath;
var newpath = '/var/www/html/Node-Js/img/' + files.filetoupload.originalFilename;
这对我有用,希望对您有所帮助