如何使用 express-uploadfile 从 POST 读取文本文件? [英] how to read text file from POST with express-uploadfile?

查看:59
本文介绍了如何使用 express-uploadfile 从 POST 读取文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

html:

<html lang="zh-cn"><头><meta charset="UTF-8"><title>文档</title><身体><h3>回车</h3><form action="/loadfile" method="post" enctype="multipart/form-data"><表格><tr><td>文件:</td><td><input type="file" name="fileName"></td></tr><tr><td><input type="submit" value="submit"></td></tr></表单></html>

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

这是节点js服务器:

var fileupload = require("express-fileupload");app.use(fileupload());app.use(bodyParser.urlencoded({extended: false}));(正在获取 html...)app.post('/loadfile', (req,res) =>{console.log("对发布操作做出反应 - loadFile");res.send("提交成功");var logFile = req.files;控制台日志(日志文件);var buffer = logFile.data;console.log(buffer.toString('utf8'));});

当我将test.txt"文件放入这个 html 时,这是服务器的控制台反应.

<预><代码>2020-02-14T08:18:50.909771+00:00 app[web.1]:对发布操作做出反应 - loadFile2020-02-14T08:18:50.912904+00:00 应用程序[web.1]:{2020-02-14T08:18:50.912905+00:00 应用程序[web.1]:文件名:{2020-02-14T08:18:50.912906+00:00 应用[web.1]:名称:'test.txt',2020-02-14T08:18:50.912907+00:00 应用程序[web.1]:数据:<缓冲区 68 65 6c 6c 6f 20 7468 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 应用程序[web.1]:大小:28,2020-02-14T08:18:50.912908+00:00 应用程序[web.1]:编码:'7bit',2020-02-14T08:18:50.912908+00:00 app[web.1]: tempFilePath: '',2020-02-14T08:18:50.912912+00:00 应用[web.1]:截断:假,2020-02-14T08:18:50.912912+00:00 app[web.1]: mimetype: 'text/plain',2020-02-14T08:18:50.912913+00:00 应用程序 [web.1]: md5: 'f4e8dbc8c1fa4d01329d4f82605111d2',2020-02-14T08:18:50.912913+00:00 app[web.1]: mv: [函数: mv]2020-02-14T08:18:50.912914+00:00 应用程序[web.1]:}**我可以正常看到上传文件的信息**2020-02-14T08:18:50.912914+00:00 应用程序[web.1]:}2020-02-14T08:18:50.914538+00:00 app[web.1]: TypeError: 无法读取属性 'to未定义的字符串2020-02-14T08:18:50.914539+00:00 应用[web.1]:在/app/server.js:111:282020-02-14T08:18:50.914540+00:00 app[web.1]: 在 Layer.handle [作为 handle_request](/app/node_modules/express/lib/router/layer.js:95:5)2020-02-14T08:18:50.914540+00:00 app[web.1]: 在下一个 (/app/node_modules/express/lib/router/route.js:137:13)2020-02-14T08:18:50.914541+00:00 app[web.1]: 在 Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3)2020-02-14T08:18:50.914541+00:00 app[web.1]: 在 Layer.handle [作为 handle_request](/app/node_modules/express/lib/router/layer.js:95:5)2020-02-14T08:18:50.914541+00:00 app[web.1]:在/app/node_modules/express/lib/ro子宫/index.js:281:222020-02-14T08:18:50.914542+00:00 app[web.1]: 在 Function.process_params (/app/node_modules/express/lib/router/index.js:335:12)2020-02-14T08:18:50.914542+00:00 app[web.1]: 在下一个 (/app/node_modules/express/库/路由器/index.js:275:10)2020-02-14T08:18:50.914542+00:00 app[web.1]: 在 urlencodedParser (/app/node_modules/body-parser/lib/types/urlencoded.js:100:7)2020-02-14T08:18:50.914543+00:00 app[web.1]: 在 Layer.handle [作为 handle_request](/app/node_modules/express/lib/router/layer.js:95:5)**但是通过toString()"读取文件的实际字符串是不可能的,为什么??**

解决方案

从文档中,您应该通过 req.files.[HTMLInput-name-attribute] 访问上传文件,您缺少路由处理程序中的 HTMLInput-name-attribute.来源.试试这个:

app.post('/loadfile', (req, res) => {console.log("对发布操作做出反应 - loadFile");res.send("提交成功");//注意添加了fileName"键//这里是input元素中的HTML名称属性值://<td><input type="file" name="fileName"></td>var logFile = req.files.fileName;控制台日志(日志文件);var buffer = logFile.data;console.log(buffer.toString('utf8'));});

I am trying to make node js server, for uploading text files there.

so I am using POST for get local user's text file, then I want to make server read the file.

I think I can make user upload he's local text file, I can get uploaded file's description.

but It is hard to make server read the file's actual string. because I always get 'undefined' when I get try to read it by logFile.data.toString('utf8');

Could you guide me how to read the uploaded text file's string?

Here is my tried code, Thank you.:

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 is a file from POST. I think logFile.data returns the byte array of its text file contents, so I tried to convert to String by toString('utf8') or toString().

This is node js server:

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'));

    });



when I put "test.txt" files to this html, this is server's console reaction.


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??**

解决方案

From the docs, you are supposed to access the upload file via req.files.[HTMLInput-name-attribute], you are missing out the HTMLInput-name-attribute in the route handler. Source. Try this:

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'));

});

这篇关于如何使用 express-uploadfile 从 POST 读取文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆