标签:
对于一个带有文件上传的表单,后台使用springMVC封装的MultipartFile file接收文件,并且需要使用异步提交,并返回相应的提示信息
使用JQUERY的form插件,即jquery.form.js这个插件,然后使用插件的ajaxSubmit方法;代码如下
1 $("#pageForm").ajaxSubmit({ 2 type: "POST", 3 url:"你的action.html", 4 dataType: "json", 5 success: function(data){ 6 if(data.msg==‘SUCCESS‘){ 7 alert(success); 8 } 9 else{ 10 alert(data.msg); 11 } 12 } 13 });
1 @RequestMapping("/insertGoodsBrand") 2 public void insertGoodsBrand( 3 HttpServletRequest request, 4 @RequestParam(value = "file", required = false) MultipartFile file,HttpServletResponse response, 5 GoodsBrand goodsBrand){ 6 JSONObject obj = new JSONObject(); 7 PrintWriter out = null; 8 try { 9 out = response.getWriter(); 10 String path = request.getSession().getServletContext().getRealPath( 11 "upload\\goods\\brand"); 12 String fileName = file.getOriginalFilename(); 13 String saveFileName = Common.getNowCorrect2Second() + "." 14 + fileName.substring(fileName.lastIndexOf(".") + 1); 15 File targetFile = new File(path, saveFileName); 16 if (!targetFile.exists()) { 17 targetFile.mkdirs(); 18 } 19 String msg = ""; 20 file.transferTo(targetFile); 21 msg = backGoodsBrandBus.insertGoodsBrand(goodsBrand, saveFileName); 22 if (msg.equals("SUCCESS")) { 23 obj.put("msg", msg); 24 } else { 25 obj.put("msg", msg); 26 } 27 out.print(obj); 28 } catch (Exception e) { 29 e.printStackTrace(); 30 } 31 finally{ 32 out.close(); 33 } 34 }
JQueryAjax使用SpringMVC中MultipartFile进行文件上传
标签:
原文地址:http://www.cnblogs.com/javahyj/p/5659329.html