问题描述
我正在使用此程序示例将图像上传到uploads/fullsize目录中.该应用程序正在运行,但是如果我尝试上传图像,则会出现以下故障:
该应用程序如下所示:
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上传->全尺寸-> images.jpg
由于body-parser
不提供文件,因此您需要多部分中间件才能使文件上载.
首先,将其添加到您的应用程序顶部:
var multipart = require('connect-multiparty');
接下来,稍微修改您的发布路线:
app.post('/upload', multipart(), function(req, res) {
// ...
}
现在req.files
现在应该可以在您的POST /upload
路线上使用了.
I am using this program example to upload a image into uploads/fullsize directory.The application is running but if I try to upload a image I am getting the following failure:
The application looks like this:
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);
});
}
});
});
Can someone help me why I am getting the TypeError??Also created my directory where the files should be save.app.jsuploads -> fullsize -> images.jpg
You need multipart middleware for your file upload to work as body-parser
does not provide it.
First, add this to the top of your app:
var multipart = require('connect-multiparty');
Next, edit your post route slightly:
app.post('/upload', multipart(), function(req, res) {
// ...
}
Now req.files
should now be avaiable to you in your POST /upload
route.
这篇关于node.js文件上传typeError无法读取属性“图像"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!