LH_Jzs
2021-04-26 dcf5a87de927e683d4bbfad3978c3d608c06e48c
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<!doctype html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <title>Document</title>
 
</head>
 
<body>
    <div>
        <input type="file" id="files" style="display: none" onchange="fileImport();">
        <input type="button" id="fileImport" value="导入">
        <input type="button" id="c" value="测试" onclick="ccccc()">
    </div>
    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
    <!-- <script src="../js/jQuery/jquery-1.11.1.js"></script> -->
    <script>
        //点击导入按钮,使files触发点击事件,然后完成读取文件的操作
        $("#fileImport").click(function () {
            $("#files").click();
        })
 
        function fileImport() {
            //获取读取我文件的File对象
            var selectedFile = document.getElementById('files').files[0];
            console.log(selectedFile);
            var reqData = {
                name: selectedFile.name,
                size: selectedFile.size,
                lastModified: selectedFile.lastModified,
                type: selectedFile.type,
                data: null
            }
            var name = selectedFile.name; //读取选中文件的文件名
            var size = selectedFile.size; //读取选中文件的大小
            var lastModified = selectedFile.lastModified; //文件时间戳
            var type = selectedFile.type;   //文件类型
            console.log("文件名:" + name + "大小:" + size);
            var reader = new FileReader(); //这是核心,读取操作就是由它完成.
            reader.readAsDataURL(selectedFile);
            // reader.readAsText(selectedFile, "utf-8"); //读取文件的内容,也可以读取文件的URL
            reader.onload = function () {
                //当读取完成后回调这个函数,然后此时文件的内容存储到了result中,直接操作即可
                console.log(this.result);
                // console.log(typeof (this.result));
                // console.log(JSON.stringify(this.result));
                reqData.data = this.result;
 
                $.ajax({
                    type: "POST",
                    url: "http://localhost:10000/SerialPortService/Test",
                    data: JSON.stringify(reqData),
                    success: function (d) {
                        console.log(d);
                    },
                    error: function (jqXHR) {
                        console.error(jqXHR);
                    }
                });
 
                //var base64String = window.btoa(this.result);
                //console.log(base64String);
                // var temp = window.atob(reqData);
                // console.log(temp);
            }
        }
        function ccccc() {
            $.ajax({
                type: "POST",
                url: "http://139.129.89.252:8080/StatisticsService.asmx/ToDaySales",
                dataType: "json",
                success: function (d) {
                    console.log(d);
                },
                error: function (jqXHR) {
                    console.log(jqXHR);
                }
            });
        }
        function arrayBufferToBase64(buffer) {
            var binary = '';
            var bytes = new Uint8Array(buffer);
            var len = bytes.byteLength;
            console.log(len);
            for (var i = 0; i < len; i++) {
                binary += String.fromCharCode(bytes[i]);
            }
            return window.btoa(binary);
        }
 
        let str = "测试", escapeStr = "";
        escapeStr = escape(str)
        console.log(escapeStr);
        console.log(unescape(escapeStr));
 
        str = "ceshi";
        escapeStr = escape(str)
        console.log(escapeStr);
        console.log(unescape(escapeStr));
    </script>
</body>
 
</html>