无法通过 nodejs 读取更新文件的属性

cannot read property of upoladed file through nodejs

本文关键字:更新 文件 属性 读取 nodejs      更新时间:2023-09-26

我一直在尝试通过 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);