node.js文件上传typeError无法读取属性'image'

时间:2014-08-23 12:23:27

标签: node.js file-upload

我正在使用此程序示例将图像上传到uploads / fullsize目录。 应用程序正在运行,但如果我尝试上传图像,则会出现以下故障:

  

TypeError:无法读取属性' image'在Object.handle中未定义   (/Users/machupicchu/Desktop/test/app.js:46:23)在next_layer   (/Users/machupicchu/Desktop/test/node_modules/express/lib/router/route.js:103:13)   在Route.dispatch   (/Users/machupicchu/Desktop/test/node_modules/express/lib/router/route.js:107:5)   在c   (/Users/machupicchu/Desktop/test/node_modules/express/lib/router/index.js:195:24)   在Function.proto.process_params   (/Users/machupicchu/Desktop/test/node_modules/express/lib/router/index.js:251:12)   在下一个   (/Users/machupicchu/Desktop/test/node_modules/express/lib/router/index.js:189:19)   在下一个   (/Users/machupicchu/Desktop/test/node_modules/express/lib/router/index.js:166:38)   在下一个   (/Users/machupicchu/Desktop/test/node_modules/express/lib/router/index.js:166:38)   在Layer.staticMiddleware [作为句柄]   (/Users/machupicchu/Desktop/test/node_modules/express/node_modules/serve-static/index.js:55:61)   在trim_prefix   (/Users/machupicchu/Desktop/test/node_modules/express/lib/router/index.js:226:17)

应用程序如下所示:

var express = require('express');
var bodyParser = require('body-parser');
var path = require ('path');
var port = 3000;
var fs = require('fs');


//var collection;
//dataExt = require('./routes/serverExtend');
// setup middleware
var app = express();
app.use(bodyParser());
app.use(express.static(__dirname + '/public')); //setup static public directory

app.set('views', __dirname + '/views'); //optional since express defaults to CWD/views
app.set('view engine', 'ejs');

// Start server
app.listen(port);
console.log('App started on port ' + port);


var form = "<!DOCTYPE HTML><html><body>" +
"<form method='post' action='/upload' enctype='multipart/form-data'>" +
"<input type='file' name='image'/>" +
"<input type='submit' /></form>" +
"</body></html>";

app.get('/', function (req, res){
    res.writeHead(200, {'Content-Type': 'text/html' });
    res.end(form);  
});

app.get('/uploads/fullsize/:file', function (req, res){
    file = req.params.file;
    var img = fs.readFileSync(__dirname + "/uploads/fullsize/" + file);
    res.writeHead(200, {'Content-Type': 'image/jpg' });
    res.end(img, 'binary');

});

/// Post files
app.post('/upload', function(req, res) {

    fs.readFile(req.files.image.path, function (err, data) {

        var imageName = req.files.image.name

        /// If there's an error
        if(!imageName){

            console.log("There was an error")
            res.redirect("/");
            res.end();

        } else {

          var newPath = __dirname + "/uploads/fullsize/" + imageName;

          /// write file to uploads/fullsize folder
          fs.writeFile(newPath, data, function (err) {

            /// let's see it
            res.redirect("/uploads/fullsize/" + imageName);

          });
        }
    });
});

有人可以帮我解释为什么我会收到TypeError吗? 还创建了我应保存文件的目录。 app.js 上传 - &gt; fullsize - &gt; images.jpg

1 个答案:

答案 0 :(得分:0)

您需要使用多部分中间件来上传文件,因为body-parser无法提供。

首先,将其添加到您应用的顶部:

var multipart = require('connect-multiparty');

接下来,稍微修改您的邮寄路线:

app.post('/upload', multipart(), function(req, res) {
   // ... 
}

现在,您req.files路径中的POST /upload现在应该可用了。