如何使用express-uploadfile从POST读取文本文件?

2024-01-10

我正在尝试制作 Node js 服务器,用于在那里上传文本文件。

所以我使用 POST 来获取本地用户的文本文件,然后我想让服务器读取该文件。

我想我可以让用户上传他的本地文本文件,我可以获取上传文件的描述。

但很难让服务器读取文件的实际字符串。 因为当我尝试阅读它时,我总是得到“未定义”logFile.data.toString('utf8');

您能指导我如何读取上传的文本文件的字符串吗?

这是我尝试过的代码,谢谢:

html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>

<h3>ENTER</h3>
<form action="/loadfile" method="post" enctype="multipart/form-data">
    <table>

        <tr>
            <td>file : </td>
            <td><input type="file" name="fileName"></td>
        </tr>
        <tr>
            <td><input type="submit" value="submit"></td>
        </tr>
    </table>
</form>

</body>
</html>

logFile 是来自 POST 的文件。 我认为 logFile.data 返回其文本文件内容的字节数组, 所以我尝试通过 toString('utf8') 或 toString() 转换为字符串。

这是节点js服务器:

var fileupload = require("express-fileupload");
  app.use(fileupload());
  app.use(bodyParser.urlencoded({extended: false}));

           (fetching html... )

app.post('/loadfile', (req,res) =>{

      console.log("react to post action - loadFile");
      res.send("submit ok");
        var logFile = req.files;

        console.log(logFile);
        var buffer = logFile.data;
        console.log(buffer.toString('utf8'));

    });



当我将“test.txt”文件放入此 html 中时,这是服务器的控制台反应。


2020-02-14T08:18:50.909771+00:00 app[web.1]: react to post action - loadFile
2020-02-14T08:18:50.912904+00:00 app[web.1]: {
2020-02-14T08:18:50.912905+00:00 app[web.1]: fileName: {
2020-02-14T08:18:50.912906+00:00 app[web.1]: name: 'test.txt',
2020-02-14T08:18:50.912907+00:00 app[web.1]: data: <Buffer 68 65 6c 6c 6f 20 74
68 69 73 20 69 73 20 74 65 73 74 20 74 65 78 74 20 66 69 6c 65>,
2020-02-14T08:18:50.912907+00:00 app[web.1]: size: 28,
2020-02-14T08:18:50.912908+00:00 app[web.1]: encoding: '7bit',
2020-02-14T08:18:50.912908+00:00 app[web.1]: tempFilePath: '',
2020-02-14T08:18:50.912912+00:00 app[web.1]: truncated: false,
2020-02-14T08:18:50.912912+00:00 app[web.1]: mimetype: 'text/plain',
2020-02-14T08:18:50.912913+00:00 app[web.1]: md5: 'f4e8dbc8c1fa4d01329d4f8260511
1d2',
2020-02-14T08:18:50.912913+00:00 app[web.1]: mv: [Function: mv]
2020-02-14T08:18:50.912914+00:00 app[web.1]: }

** I can see the uploaded file's information properly **

2020-02-14T08:18:50.912914+00:00 app[web.1]: }
2020-02-14T08:18:50.914538+00:00 app[web.1]: TypeError: Cannot read property 'to
String' of undefined
2020-02-14T08:18:50.914539+00:00 app[web.1]: at /app/server.js:111:28
2020-02-14T08:18:50.914540+00:00 app[web.1]: at Layer.handle [as handle_request]
 (/app/node_modules/express/lib/router/layer.js:95:5)
2020-02-14T08:18:50.914540+00:00 app[web.1]: at next (/app/node_modules/express/
lib/router/route.js:137:13)
2020-02-14T08:18:50.914541+00:00 app[web.1]: at Route.dispatch (/app/node_module
s/express/lib/router/route.js:112:3)
2020-02-14T08:18:50.914541+00:00 app[web.1]: at Layer.handle [as handle_request]
 (/app/node_modules/express/lib/router/layer.js:95:5)
2020-02-14T08:18:50.914541+00:00 app[web.1]: at /app/node_modules/express/lib/ro
uter/index.js:281:22
2020-02-14T08:18:50.914542+00:00 app[web.1]: at Function.process_params (/app/no
de_modules/express/lib/router/index.js:335:12)
2020-02-14T08:18:50.914542+00:00 app[web.1]: at next (/app/node_modules/express/
lib/router/index.js:275:10)
2020-02-14T08:18:50.914542+00:00 app[web.1]: at urlencodedParser (/app/node_modu
les/body-parser/lib/types/urlencoded.js:100:7)
2020-02-14T08:18:50.914543+00:00 app[web.1]: at Layer.handle [as handle_request]
 (/app/node_modules/express/lib/router/layer.js:95:5)

**But read the file's actual string by 'toString()' is not possible, why??**


从文档中,您应该通过访问上传文件req.files.[HTMLInput-name-attribute],你错过了HTMLInput-name-attribute在路线处理程序中。Source https://www.npmjs.com/package/express-fileupload#usage。尝试这个:

app.post('/loadfile', (req, res) => {

  console.log("react to post action - loadFile");
  res.send("submit ok");
  // Notice the addition of the "fileName" key
  // It is the HTML name attribute value here in the input element:
  // <td><input type="file" name="fileName"></td>
  var logFile = req.files.fileName;

  console.log(logFile);
  var buffer = logFile.data;
  console.log(buffer.toString('utf8'));

});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用express-uploadfile从POST读取文本文件? 的相关文章

随机推荐