我一直试图通过tutorialspoint.com上的NodeJS教程顺利上传文件。文件上传到用于保存上载文件的文件夹,但文件不是原始名称,而是重命名为其他值,如哈希值或时间戳。我得到的错误如下所示。
如果有人能告诉我文件是如何上传的话也会很棒,这样我就能更好地理解我所犯的任何错误。
以下是错误的详细信息:
TypeError: Cannot read property 'file' of undefined
at /Applications/XAMPP/xamppfiles/htdocs/NodeJS_Tutorialspoint/Tutorial7_Express/server_upload.js:18:26
at Layer.handle [as handle_request] (/Applications/XAMPP/xamppfiles/htdocs/NodeJS_Tutorialspoint/Tutorial7_Express/node_modules/express/lib/router/layer.js:95:5)
at next (/Applications/XAMPP/xamppfiles/htdocs/NodeJS_Tutorialspoint/Tutorial7_Express/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/Applications/XAMPP/xamppfiles/htdocs/NodeJS_Tutorialspoint/Tutorial7_Express/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Applications/XAMPP/xamppfiles/htdocs/NodeJS_Tutorialspoint/Tutorial7_Express/node_modules/express/lib/router/layer.js:95:5)
at /Applications/XAMPP/xamppfiles/htdocs/NodeJS_Tutorialspoint/Tutorial7_Express/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/Applications/XAMPP/xamppfiles/htdocs/NodeJS_Tutorialspoint/Tutorial7_Express/node_modules/express/lib/router/index.js:330:12)
at next (/Applications/XAMPP/xamppfiles/htdocs/NodeJS_Tutorialspoint/Tutorial7_Express/node_modules/express/lib/router/index.js:271:10)
at Immediate.<anonymous> (/Applications/XAMPP/xamppfiles/htdocs/NodeJS_Tutorialspoint/Tutorial7_Express/node_modules/multer/lib/make-middleware.js:52:37)
at Immediate.immediate._onImmediate (timers.js:440:18)
文件上传过程的程序:
index_upload.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:8081/file_upload" method="POST" enctype="multipart/form-data">
<input type="file" name="filename" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>
server_upload.js:
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: 'Uploads/'}).single('filename'));
app.get('/index_upload.html', function (req, res)
{
res.sendFile( __dirname + "/" + "index_upload.html" );
})
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(8081, function ()
{
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})
答案 0 :(得分:1)
您正在HTML
上传递Name =“filename”console.log(req.files.filename.name);
console.log(req.files.filename.path);
console.log(req.files.filename.type);