2016-02-03 60 views
5

我剛剛學習node.js,並且難以使用express和multer進行簡單的文件上載。Node.js TypeError:無法讀取未定義的屬性「文件」

這裏是形式:

上傳圖片

在我configure.js我:

app.use(express.static(path.join(__dirname, 'public'))); 
app.use(multer({dest:'../public/upload/temp'}).single('file')); 

image.js控制器:

create: function(req, res) { 
     var saveImage = function() { 
      console.log(req.body); 
      var possible = 'abcdefghijklmnopqrstuvwxyz', 
       imgUrl = ''; 

      for(var i=0; i < 6; i+=1) { 
       imgUrl += possible.charAt(Math.floor(Math.random() * possible.length)); 
      } 

      var tempPath = req.files.file.path, //<line 55 error 
       ext = path.extname(req.files.file.name).toLowerCase(), 
       targetPath = path.resolve('./public/upload/' + imgUrl + ext); 

      if (ext === '.png' || ext === '.jpg' || ext === '.jpeg' || ext === '.gif') { 


       fs.rename(tempPath, targetPath, function(err) { 
        if (err) throw err; 

        res.redirect('/images/' + imgUrl); 
       }); 
      } else { 
       fs.unlink(tempPath, function() { 
        if (err) throw err; 

        res.json(500, {error: 'Only image files are allowed.'}); 
       }); 
      } 
     }; 

     saveImage(); 
    }, 

不過,我當我嘗試上傳的圖像得到這個錯誤:

TypeError: Cannot read property 'file' of undefined 
    at saveImage (/home/pc/node-dev/test-proj/controllers/image.js:55:37) 
    at module.exports.create (/home/pc/node-dev/test-proj/controllers/image.js:76:9) 
    at Layer.handle [as handle_request] (/home/pc/node-dev/test-proj/node_modules/express/lib/router/layer.js:95:5) 
    at next (/home/pc/node-dev/test-proj/node_modules/express/lib/router/route.js:131:13) 
    at Route.dispatch (/home/pc/node-dev/test-proj/node_modules/express/lib/router/route.js:112:3) 
    at Layer.handle [as handle_request] (/home/pc/node-dev/test-proj/node_modules/express/lib/router/layer.js:95:5) 
    at /home/pc/node-dev/test-proj/node_modules/express/lib/router/index.js:277:22 
    at Function.process_params (/home/pc/node-dev/test-proj/node_modules/express/lib/router/index.js:330:12) 
    at next (/home/pc/node-dev/test-proj/node_modules/express/lib/router/index.js:271:10) 
    at urlencodedParser (/home/pc/node-dev/test-proj/node_modules/body-parser/lib/types/urlencoded.js:95:37) 

當我登錄了req對象,file是不是有:

{ title: 'myimage', description: 'something' } 

實際上,這個片段只是我在this book中讀到的一個稍微修改過的版本,它使用的是過時的express-3。所以基本上我只是用multer部分進行了更新。

我想知道這裏有什麼問題,以及如何解決它。

+0

你有沒有找到一個可行的解決方案,這與穆勒?我嘗試了下面列出的修復程序,並將其更改爲req.file和req.path似乎可行,但現在本書中的示例重定向到/ image/route,但未成功命名或接收該文件。 – Dave

+0

You var tempPath = req.file.path; var ext = path.extname(req.file.originalname).toLowerCase(); var targetPath = path.resolve('./ public/upload /'+ imgUrl + ext); – iansari

回答

9

您正在使用upload.single,您應該使用req.file而不是req.files。要上傳多個文件,請使用upload.array

請注意,您在req.file之後不需要另一個.filereq.file是上傳的文件,如果您使用的是upload.single

+0

感謝您的提示。然而,現在我使用'tempPath = req.file.file.path,'我導致'TypeError:無法讀取未定義錯誤的屬性'路徑'。有任何想法嗎? – qliq

+1

@qliq [如文檔中所述](https://www.npmjs.com/package/multer#singlefieldname),您只需要使用'req.file.path',而不是'req.file.file.path' 。 (來自文檔:單個文件將被存儲在'req.file'中。) – maowtm

+0

簡短而清晰..感謝好友 – Jana

相關問題