1、字典表中添加白名单 value文件格式
INSERT INTO `pt_dictionary` (`id`,`sign`,`code`,`name`,`value`, `sort`,`isDefault`,`state`,`active`) VALUES ('whiteList','whiteList',0,'上传文件格式格式白名单','.png.jpg.xsls.log',99,0,1,1);
一、前端
2、common模块添加查询白名单方法pmp.js pt.varification方法
pt.varification(fileName)方法
fileName:文件名
回参 true:格式通过
false:格式不通过
3、js中上传前调用白名单方法,进行文件格式对比,不在白名单内提示白名单中得格式
//格式验证 if(!pt.verification(fileName)){ return; } //多个 for(var i=0;i<files.length;i++){ if(!pt.verification(files[i].name)){ return; } }
//演示js
function hosue_uploadAttachment(par) { var fileName = $("#selectAttachment").val(); if (fileName == null) { return; } var state = pt.verification(fileName);//格式验证 if(!state){ return; } $('#fileForm').ajaxSubmit({ url : pt.base + "sb/sbtz/uploadAttachment.do", type : 'post', dataType : 'json', async : false, success : function(result) { pt.showMsg(result.msg); var list = result.data; if (list != null) { $("#loadFileId").val(list[0]); $("#fileDiv").val(""); $("#fileDiv").val(list[3] + "个文件"); } } }); }
//格式验证方法
pt.verification = function(fileName){ var state; var format = fileName.substr(fileName.lastIndexOf(".")+1,fileName.length); $.ajax({ async: false, type : 'post', url : pt.base + "pt/dict/queryWhiteList.do", data : { "format" : format }, dataType : "json", success : function(result) { if(result.state){ state = true; }else{ pt.showErrorMsg("请选择"+result.data+"格式文件!"); state = false; } } }); return state; }
// 获取白名单信息并判断
@ResponseBody @OperationLog(rank = RankLevelEnum.COMMON, module = ModuleEnum.COMMON, entity = "ptDictionary", name = "queryWhiteList", type = OperatonTypeEnum.QUERY, title = "查询字典下拉框") @RequestMapping(value = "queryWhiteList", method = RequestMethod.POST) public AjaxResult queryWhiteList(HttpServletRequest request,HttpServletResponse response) { AjaxResult result = new AjaxResult(); String format = request.getParameter("format"); PtDictionaryEntity ptDictionaryEntity = ptDictionaryService.findById("whiteList"); String[] splits = StringUtils.split(ptDictionaryEntity.getValue(), "."); List<String> strings = Arrays.asList(splits); result.setState(strings.contains(format)); result.setData(ptDictionaryEntity.getValue()); return result; }