废话不多说,直接上代码
js:
layui.use(‘upload‘, function(){ var $ = layui.jquery,upload = layui.upload; //绑定原始文件域 upload.render({ elem: ‘#test20‘ ,url: "<c:url value=‘/order/imgUpload.do?proVal=IMG_PATH‘/>" ,auto: true ,accept: ‘file‘ ,exts:"jpg|png|gif|jpeg|bmp" ,size:1024 ,before: function(obj){ var orderID = $("#orderID").val(); this.data={"orderID":orderID}//携带动态参数 } ,done: function(res){ if(res.msg==‘ok‘){ alert(‘上传成功‘); }else{ alert(res.msg); } } }); });
java:
上传文件的代码我没写,主要写了怎么取这个动态参数的值
@ResponseBody
@RequestMapping("/imgUpload") public JsonResult imgUpload( HttpServletRequest request,HttpServletResponse response,ModelMap map ){ JsonResult json = new JsonResult(); Integer orderID = null;//订单ID DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(10240000); try { ServletFileUpload upload = new ServletFileUpload(factory); upload.setHeaderEncoding("utf-8"); @SuppressWarnings("rawtypes") List fileList = null; try { fileList = upload.parseRequest(request); } catch (FileUploadException ex) { return null; } @SuppressWarnings("unchecked") Iterator<FileItem> it = fileList.iterator(); //取参数 while (it.hasNext()) { FileItem item = it.next(); if(item.isFormField()&&"orderID".equals(item.getFieldName())){ orderID = Integer.valueOf(item.getString()); } }
if(orderID==null){ json.setMsg("参数有误,请刷新重试!"); return json; } } catch (Exception e) { e.printStackTrace(); json.setMsg("上传失败"); }finally{ factory = null; } return json; }