一、前端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);
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);
}
})
二、后台接口内容
@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);
String fileName = "explay"+"."+prefix;
File targetFile = new File(path, fileName);
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;
}