注意: - 堆栈社区的标准日益恶化,你们这些人 甚至没有在标记之前阅读问题和答案 的重复
首先阅读问题和答案,然后说重复。我准备好了 使用该答案(即使用
multer
模块)。你不能看到代码。这真令人沮丧。由于这些类型的事情,即使是符合条件的问题也没有得到回答或没有得到足够的重视。
我正在关注NodeJs https://www.tutorialspoint.com/nodejs/nodejs_express_framework.htm的本教程。在运行此示例时,我遇到了此问题 Internal Server Error
。
这是HTML代码:
<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action = "http://127.0.0.1:8050/file_upload" method = "POST"
enctype = "multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type = "submit" value = "Upload File" />
</form>
</body>
</html>
这是NodeJs代码: -
var express = require('express');
var app = express();
var fs = require("fs");
var bodyParser = require('body-parser');
var multer = require('multer');
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(multer({ dest: '/tmp/'})).single('file'));//I have added this, this is also a mistake by them (tutorialpoint.com)
app.get('/index.htm', function (req, res) {
res.sendFile( __dirname + "/" + "index.htm" );
})
app.post('/file_upload', function (req, res) {
console.log(req.files.file.name);
console.log(req.files.file.path);
console.log(req.files.file.type);
var file = __dirname + "/" + req.files.file.name;
fs.readFile( req.files.file.path, function (err, data) {
fs.writeFile(file, data, function (err) {
if( err ){
console.log( err );
}else{
response = {
message:'File uploaded successfully',
filename:req.files.file.name
};
}
console.log( response );
res.end( JSON.stringify( response ) );
});
});
})
var server = app.listen(8050, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})
这是我的日志: -
Example app listening at http://localhost:8050
TypeError: Cannot read property 'file' of undefined
at C:\Users\devuser\Desktop\nodejsPrograms\filenew\server.js:17:25
at Layer.handle [as handle_request] (C:\Users\devuser\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\devuser\node_modules\express\lib\router\route.js:131:13)
at Route.dispatch (C:\Users\devuser\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\devuser\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\devuser\node_modules\express\lib\router\index.js:277:22
at Function.process_params (C:\Users\devuser\node_modules\express\lib\router\index.js:330:12)
at next (C:\Users\devuser\node_modules\express\lib\router\index.js:271:10)
at Immediate.<anonymous> (C:\Users\devuser\node_modules\multer\lib\make-middleware.js:53:37)
at Immediate.immediate._onImmediate (timers.js:440:18)
答案 0 :(得分:2)
来自文档
<强>
中.single(fieldname)
强>
接受名为fieldname的单个文件。单个文件将存储在req.file
。
将req.files.file
更改为req.file
,它会起作用。 file
不是字段的名称,而是单个上传而不是files
。
答案 1 :(得分:1)
Lorenz Meyer回答的内容是正确的。我使其更清晰。删除files属性,它必须工作。 这是您更新的服务器代码:
var express = require('express');
var app = express();
var fs = require("fs");
var bodyParser = require('body-parser');
var multer = require('multer');
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(multer({ dest: '/tmp/'}).single('file'));//I have added this, this is also a mistake by them (tutorialpoint.com)
app.get('/index.htm', function (req, res) {
res.sendFile( __dirname + "/" + "index.htm" );
})
app.post('/file_upload', function (req, res) {
console.log(req.file.name);
console.log(req.file.path);
console.log(req.file.type);
var file = __dirname + "/" + req.file.name;
fs.readFile( req.file.path, function (err, data) {
fs.writeFile(file, data, function (err) {
if( err ){
console.log( err );
}else{
response = {
message:'File uploaded successfully',
filename:req.file.name
};
}
console.log( response );
res.end( JSON.stringify( response ) );
});
});
})
var server = app.listen(8050, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})