TypeError:无法读取未定义的属性“image” [英] TypeError: Cannot read property 'image' of undefined

查看:759
本文介绍了TypeError:无法读取未定义的属性“image”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的源代码有问题,但我无法弄清楚什么 - 请帮忙。我正在寻找一些解决方案找到一些和更新的源代码,但没有帮助。

There is something wrong in my source code but I'm not able to figure out what - please help. I was looking for some solutions found some and updated source code according them but didn't help.

var express = require('express');
var fs = require('fs');
var bodyParser  = require('body-parser');
var app = express()

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

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



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

});

app.post('/upload', function(req, res){
    fs.readFile(req.files.image.path, function(err, data){

        var imageName = req.files.image.name
        if(!imageName){
            console.log("There was an error");
            res.redirect('/');
            res.end();
        }else{
            var newPath = __dirname + "/uploads/fullsize/" + imageName;

            fs.writeFile(newPath, data, function(err){
                res.redirect("/uploads/fullsize/" + imageName);
            });
        }

    });
});

app.listen(8080);


推荐答案

body-parser 中间件不处理多部分正文。

正文-parser github:

This does not handle multipart bodies, due to their complex and typically large
nature. For multipart bodies, you may be interested in the following modules:



查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆