layui上传word文档流程步骤

一、前端html

<button class="layui-btn demoMore">上传模板文件</button>

二、前端js内容

 // 上传模板  
	        upload.render({
	          elem: '.demoMore'
	          ,url: '${pageContext.request.contextPath }/headword/SaveFileDocx'
	          ,auto: true
	          , accept:'file'
	          
	          ,done: function(res, index, upload){
	        	  console.log(res); //得到文件索引
	              console.log(index); //得到文件对象
	              console.log(upload); //得到文件base64编码,比如图片
	        	  if(res.code==6)
	        		  {
	        		  layer.msg('上传成功',{icon:6,time:1000},function(){
	  					//刷新本页面
	  					location.reload();
	  			});
	        		  }
	        	  else if(res.code==5){
	        		  layer.msg('上传失败',{icon:5,time:1000},function(){
	    					//刷新本页面
	    					location.reload();
	    			});
	        	  }
	            var item = this.item;
	            console.log(item); //获取当前触发上传的元素,layui 2.1.0 新增
	          }
	        })

二、后台接口内容

  //新模板上传docx文件
	     @ResponseBody
	     @RequestMapping(value="/SaveFileDocx")
	   	public JSONObject saveFile(@RequestParam("file")MultipartFile attach, HttpServletRequest request, HttpServletResponse response) throws Exception		
	   	{ 
	   
	        String carPictuerUrl = null; //上传文件指定的位置
	 		//判断文件是否为空
	 		if(!attach.isEmpty()){
	 			
	 			  String path1 =
	 			  request.getSession().getServletContext().getRealPath("/");
	 			 
	 			String path = request.getSession().getServletContext().getRealPath("statics/uploadfiles");
	 			
	 			System.out.println("path1:==============="+path1);
	 			System.out.println("path======"+path);
	 			String oldFileName = attach.getOriginalFilename();//原文件名
	 			String prefix=FilenameUtils.getExtension(oldFileName);//原文件后缀
	 			/**
	 			 * RandomUtils.nextInt(1, 1000000)
	 	         * 返回一个在指定区间内的整数
	 	         * startInclusive 可以返回的最小值必须是非负的
	 	         * endExclusive 上限(不包括)
	 	         */
	 			//String fileName = System.currentTimeMillis()+RandomUtils.nextInt(1000000)+"."+prefix;  	
	 			//String fileName = System.currentTimeMillis()+RandomUtils.nextInt(1,1000000)+"."+prefix;
	 		    //设置上传文件的新名称
	 			String fileName = "explay"+"."+prefix;
	 			File targetFile = new File(path, fileName);  //创建文件
			/*
			 * if(!targetFile.exists()){ //判断文件夹是否存在 targetFile.mkdirs(); }
			 */ 
	 			 
	 				try {
	 					attach.transferTo(targetFile);
	 				} catch (IllegalStateException e) {
	 					e.printStackTrace();
	 				} catch (IOException e) {
	 					e.printStackTrace();
	 				}
	 		
	 			 
	 				carPictuerUrl = "statics/uploadfiledocxs/"+fileName;
	 		}
	 	
	        
	 		 JSONObject  jsonObject=new JSONObject();
	 		     jsonObject.put("msg", "保存成功");
		 		 jsonObject.put("code", 6);
		 		 jsonObject.put("src",carPictuerUrl);
		 		 
		    return jsonObject;	
	   	}
	     
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

曦月合一

你的鼓励是我们前进的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值