关于javascript:将excel文件数据发送到Node.js服务器并将其解析为json

Send excel file data to Node.js server and parse it into json

我对node.js相对较新,但是对javascript的了解不是那么好。但是我想做的是将.xlsx文件发送到node.js,然后将其转换为json。但我的要求正文始终是{}。我的过程是,我要让用户选择一个excel文件,然后在其表单字段上单击"提交",然后让服务器端将该excel文件转换为json。

我的代码:
HTML:

1
2
3
4
                            <form name="myForm" action="sendFile" method="post" enctype="multipart/form-data">
                                <input id="fileName" name="fileName" type="file"  class="validate">
                                <input type="submit">
                            </form>

我的服务器代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
app.post("/sendFile", function(req, res){
    console.log("--------------in batch create user--------------");
    console.log("body is:"+ req.body);
    console.log(req.body.fileName);  // is undefined
    console.log(req.body); //is {}

    //I tried this as a second option but this didnt work either....

    // var form = new formidable.IncomingForm();
    // form.parse(req, function (err, fields, files) {
    //     console.log(files);
    //     console.log(fields);
    //     console.log(files.filetoupload);
    //     var oldpath = files.filetoupload.path;
    //
    //     convertExcel = require('excel-as-json').processFile;
    //     convertExcel(oldpath, null, null, function(err, data){
    //         console.log("----in convert excell ---------");
    //         console.log("data for excel is:"+data);
    //
    //     });
    // });

});

如果您使用Express,则需要一个中间件来解析请求正文并
multer库以处理多部分/表单数据内容。在此处链接图书馆。

以下是您的情况的简短示例:

obs:将Excel转换为Json的部分,您可以使用问题库或node-excel-to-json。

index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
   
</head>
<body>

                            <form name="myForm" action="/sendFile" method="post" enctype="multipart/form-data">
                                <input id="fileName" name="fileName" type="file"  class="validate">
                                <input type="submit">
                            </form>
                       
</body>
</html>

server.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
var fs = require("fs");
var express = require("express");
var bodyParser = require('body-parser');
var multer = require("multer");
var upload = multer();

var app = express();

// for parsing application/json
app.use(bodyParser.json());

// for parsing application/xwww-
app.use(bodyParser.urlencoded({ extended: true }));
//form-urlencoded

// for parsing multipart/form-data
//app.use(upload.array());

//static folder
app.use(express.static('public'));


app.get('/', function(req, res) {
    fs.readFile('index.html', 'utf8', function (err,data) {
    if (err) {
        return console.log(err);
    }
        //console.log(data);
        res.write(data)
    });
});

app.post("/sendFile",  upload.single('fileName'), function(req, res){

    //text fields
    console.log(req.body);

    //file contents
    console.log(req.file);


    // process
    var response = 'Do something';
    res.json(response);

});

app.listen(8000, function () {
  console.log('Example app listening on port 8000!')
});