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?
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屋!