TypeError:上传文件

时间:2018-11-30 09:22:58

标签: javascript node.js formidable

我是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);

在这里我已经安装了强大的模块。 我收到以下错误消息,请看看。

/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)

3 个答案:

答案 0 :(得分:2)

您在console.log(files)中得到 null {} 是此特定错误的原因。 Files为空,因此files.filetoupload将不确定,这就是为什么找不到files.filetoupload.path的原因。我想说的是,请找出为什么files为空。一旦开始获取文件中的数据,此错误将得到解决。

答案 1 :(得分:0)

使用文件[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);

答案 2 :(得分:0)

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

我用HTML命名了文件元素

name =“发票” //输入类型=文件

server.js 中使用

文件上传

因此,如果有人遇到类似问题,请查看您的代码,尤其是给HTML元素指定的名称。