javascript - 无法通过nodejs读取upoladed文件的属性

标签 javascript html node.js

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

最佳答案

您正在 HTML 上传递 Name = "filename"

console.log(req.files.filename.name);
console.log(req.files.filename.path);
console.log(req.files.filename.type);

关于javascript - 无法通过nodejs读取upoladed文件的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35590880/

相关文章:

javascript - 如何验证或包装用户输入的 HTML 以修复未关闭的标签

javascript - 使用 onClick 复制电子邮件表单

javascript - 即使单个图像存在单项模式, Owl Carousel 也会自动刷新

javascript - 占位符获取第一个单词并忽略其他单词

javascript - 如何使用 sitemap.js?

javascript - 深层对象字面量的简写?

数据对象的 Javascript dom 操作

javascript - 如何设置 .animate 函数 vanilla JS 参数中的动画填充模式?

php - 在自定义托管结帐页面上保护/验证 Paypal Payments Pro 数据并将自定义数据提交给 PayPal

node.js - 错误/node_modules/node-sass : Command failed