如何使用快速上传文件从开机自检中读取文本文件?



我正在尝试制作节点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.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。源。试试这个:

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

最新更新