TypeError:无法读取未定义的属性“image” [英] TypeError: Cannot read property 'image' of undefined
本文介绍了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:
- <一个href =https://www.npmjs.org/package/busboy#readme =nofollow> busboy 和 connect-busboy
- multiparty 和 connect-multiparty
- 强大的
- multer
- busboy and connect-busboy
- multiparty and connect-multiparty
- formidable
- multer
https://github.com/expressjs/body-parser
这篇关于TypeError:无法读取未定义的属性“image”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文