文件导入代码

这篇博客主要介绍了如何在控制层实现文件导入的功能,重点提及了在页面中使用WebUploader.js这一关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

控制层

/**
 *  职级设置--批量导入
 * @param filePath
 * @return
 * @throws ParseException
 */
@RequestMapping("/zjimportLiZhi")
@ResponseBody
public AjaxResult importNdd(String filePath) throws ParseException{
	StringBuffer error = new StringBuffer("");
    List<List<String>> dataList = ExcelUtils.readXlsx(filePath, 1);
    if (dataList != null && dataList.size() > 0) {
    	int i = 1;
    	//遍历
        for (List<String> data : dataList) {
        	
            String str = "第" + (i + 1) + "行";
            
            if(data.size() >=1 && data.get(0) == null || data.get(0).length() == 0) {
            	return AjaxResult.fail(error.append(str+"职级名称不能为空").toString());
            }
            String	gh1=data.get(0).toString();
            
            //保存到数据库
            Rs2Zj rs2Zj=new Rs2Zj();
            rs2Zj.setMc(data.get(0).toString());
            zjService.saveRs2Zj(rs2Zj);
            i++;
        }
        return AjaxResult.success("导入成功");
    }
    else{
 	   return AjaxResult.fail("网路异常,稍后再试");
    }
	
}

页面

记得一定要引 webuploader.js

  //导入验证
    $(function(){
    	questProject();
        var webuploaderLoadIndex;
        var uploader = WebUploader.create({
            // swf文件路径
            swf: './../static/plugins/webuploader/Uploader.swf',
            // 文件接收服务端。
            server: './../upload/ajax/upload_file',
            // 选择文件的按钮。可选。
            // 内部根据当前运行是创建,可能是input元素,也可能是flash.
            pick: '#uploadfileinput',  ///这是点击按钮的ID
            // 不压缩image, 默认如果是jpeg,文件上传前会压缩一把再上传!
            resize: false,
            formData: {'operationType': 'qgzxinsertExcl'},
            accept: {
                title: 'excel',
                extensions: 'xls,xlsx',
                mimeTypes: ''
            }
        });
        
        // 当有文件被添加进队列的时候
        uploader.on('fileQueued', function (file) {
            webuploaderLoadIndex = layer.load(1, {shade: [0.3, '#000']});
            uploader.upload();
        });
        
        uploader.on('uploadSuccess', function (file, ret, hid) {
            console.log(ret);
            if (!ret.success) {
                layer.close(webuploaderLoadIndex);
                layer.alert(ret.msg ? ret.msg : '文件上传失败', {icon: 0, closeBtn: 0}, function () {
                    window.location.reload();
                });
                return false;
            }
            layer.close(webuploaderLoadIndex);
            $.ajax({
                type: 'post',
                url: '${pageContext.request.contextPath}/rs2zj/zjimportLiZhi',
                data: {
                	filePath: ret.data
                },
                dataType: 'json',
                beforeSend: function () {
                    loading = layer.load(1, {shade: [0.1, '#fff']});
                },
                success: function (data) {
                    if (data.success) {
                        layer.alert(data.msg, {icon: 1, closeBtn: 0}, function () {
                            window.location.reload();
                        })
                    } else {
                        layer.alert(data.msg ? data.msg : '上传失败,请检查模板数据是否正确', {icon: 0, closeBtn: 0}, function () {
                            window.location.reload();
                        }) 
                    }
                },
                error: function (err) {
                    alert("服务器异常,请稍后再试");
                },
                complete: function () {
                    layer.close(loading);
                }
            });
            $('#' + file.id).find('p.state').text('已上传');
        });
 	        uploader.on('uploadError', function (file) {
            layer.alert('上传出错', {icon: 0, closeBtn: 0}, function () {
                window.location.reload();
            })
        });
        //测试------------
    
    })
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值