簡體   English   中英

TypeError:上傳文件時無法讀取Node js中未定義的屬性“路徑”

[英]TypeError: Cannot read property 'path' of undefined in Node js while uploading file

我是節點 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)

您在console.log(files)中得到null{}是此特定錯誤背后的原因。 Files即將為空,因此files.filetoupload將是未定義的,這就是無法找到files.filetoupload.path的原因。 我會說請找出為什么files變為空。 一旦您開始在文件中獲取數據,這個錯誤就會得到解決。

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

我遇到了同樣的問題,在仔細查看我的代碼后,我發現我使用了不同的名稱來獲取server.js 中的文件,然后是我在html 文件中提到的名稱

我在 HTML 中命名文件元素

name="invoices" // 輸入類型=文件

server.js中使用

上傳文件

因此,如果有人遇到類似問題,請查看您的代碼,尤其是 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;

這對我有用,所以我希望它有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM