无法通过 nodejs 读取更新文件的属性
cannot read property of upoladed file through nodejs
我一直在尝试通过 NodeJS 教程顺利上传文件而不会遇到任何问题 tutorialspoint.com。文件将上传到旨在保存上传文件的文件夹中,但文件不是其原始名称,而是重命名为其他值,如哈希值或时间戳。我得到的错误如下所示。
如果有人可以告诉我文件是如何上传的,这样我就可以更好地理解我所犯的任何错误,那就太好了。
以下是错误的详细信息:
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)
})
你在 HTML 上传递 Name = "filename"
console.log(req.files.filename.name);
console.log(req.files.filename.path);
console.log(req.files.filename.type);
相关文章:
- 通过CSV文件上载更新数据库表
- 如何检查Json文件更新,如果更新了,则用更新的数据刷新我的页面
- Glassfish没有更新JavaScript文件
- JavaScript 不会更新文件
- 如何更新文件中的 JSON 数据
- 从javascript运行php更新文件
- 通过 IFrame 更新文件时的错误处理
- 无法通过 nodejs 读取更新文件的属性
- 节点.js更新文件中的 JSON 数据
- 露天更新文件 - 错误开机自检
- 为对事件更新文件的相同调用刷新浏览器缓存
- 在javascript中部分更新文件上传页面
- 上传或更新文件匹配表中行的objectId
- Filereader API在第一次更改后没有更新文件
- 更新文件时不需要重启Node.js中的服务器
- 插入和更新文件在谷歌驱动器与javascript
- 驱动器API:更新文件时的DeadlineExceededException
- 更新文件时触发来自 gulpfile 的更新
- Ajax打开(post)不更新文件
- 如果修改了Nginx更新文件