1. 程式人生 > >Layer文件上傳操作

Layer文件上傳操作

重試 pan man 回調 get src puts shm tar

1:upload.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="./layui/css/layui.css" media="all">
</head>
<body>
    <div class="layui-container">
        <div class="layui-row"
align="center" style="margin-top: 30px;"> <button type="button" class="layui-btn" id="upload"> <i class="layui-icon">&#xe67c;</i>選擇文件</button> </div> <div class="layui-row" align="center" style="margin-top: 30px;"> <
textarea id="result" cols="50" rows="10"></textarea> </div> </div> </body> <script src="./jquery/jquery.min.js"></script> <script src="./layui/layui.js"></script> <script> layui.use(upload, function(){ var upload = layui.upload;
//執行上傳 var uploadInst = upload.render({ elem: #upload //綁定元素 ,url: /ssfwpt/ra/ramanage //上傳接口 ,method: POST ,accept: file ,size: 50 ,before: function(obj){ layer.load(); } ,done: function(res){//上傳完畢回調 layer.closeAll(loading); var result = ‘‘; for(var i=0; i<res.length; i++){ result = result + res[i].nsrsbh+"="+res[i].container+"\n"; } $("#result").html(result); } ,error: function(){//請求異常回調 layer.closeAll(loading); layer.msg(網絡異常,請稍後重試!); } }); }); </script> </html>

2:後臺(Spring-boot)

/**
     * 實現文件上傳
     * */
    @RequestMapping(value = "/ramanage", method = RequestMethod.POST)
    @ResponseBody
    public List<Map<String,String>> ramanage(@RequestParam("file") MultipartFile file){
        List<Map<String,String>> result = new ArrayList<>();

        try {
            InputStream input = file.getInputStream();

            Workbook wb = new HSSFWorkbook(input);

            Sheet sheet = wb.getSheetAt(0);

            int rowNum = sheet.getLastRowNum()+1;

            Map<String,String> map;
            for(int i=1; i<rowNum; i++){
                Row row = sheet.getRow(i);

                //容器名稱
                Cell containerCell = row.getCell(0);
                String container = containerCell.getStringCellValue();

                //稅號
                Cell nsrsbhCell = row.getCell(1);
                String nsrsbh = nsrsbhCell.getStringCellValue();

                map = new HashMap<>();
                map.put("nsrsbh", nsrsbh);
                map.put("container", container);

                result.add(map);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

Layer文件上傳操作