Layui框架实现图片上传

Layui框架实现图片上传

前言:

一直以来,图片上传总是件很麻烦的事。最近在学layui,发现layui真是极大简化了各种复杂的操作,避免了繁琐的开发。
layui图片上传和传统的图片上传不同,它并不予表单元素并存,而是单独通过异步来上传到后端,继而进行之后的操作。所以,编写表单代码时,并不需要添加enctype=“multipart/form-data”input type=“file” 相关组件了,直接写最原始的表单就行。

附上完整demo:https://github.com/BraisedPanda/StudentSystem2.5
demo里面新建人员中有图片导入功能,static下的sql文件夹里有完整的数据库表和测试数据,登录界面登录名:zhangsan 密码:123

前端代码:


                    

tips:
Layui框架实现图片上传_第1张图片
1、白色的是隐藏的输入框,用来传递images的src地址的,因为图片不同于表单一起传数据,所以只能借助js代码来绑定参数
2、黄色框子里就是上传图片的组件了,可以把它夹在form表单中

js代码


tips
Layui框架实现图片上传_第2张图片
1、js代码重点是黄色框子里的内容
fileupload.attr(“value”,res.data.src);
res.data.src是获取后端传过来的图片的url,之后给表单的隐藏图片输入框赋值即可。

后端代码:

 //图片上传测试
    @ResponseBody
    @RequestMapping("upload")
    public Map upload(MultipartFile file,HttpServletRequest request){

        String prefix="";
        String dateStr="";
        //保存上传
        OutputStream out = null;
        InputStream fileInput=null;
        try{
            if(file!=null){
                String originalName = file.getOriginalFilename();
                prefix=originalName.substring(originalName.lastIndexOf(".")+1);
                Date date = new Date();
                String uuid = UUID.randomUUID()+"";
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                dateStr = simpleDateFormat.format(date);
                String filepath = "D:\\mycode\\LayUiTest\\src\\main\\resources\\static\\images\\" + dateStr+"\\"+uuid+"." + prefix;


                File files=new File(filepath);
                //打印查看上传路径
                System.out.println(filepath);
                if(!files.getParentFile().exists()){
                    files.getParentFile().mkdirs();
                }
                file.transferTo(files);
                Map map2=new HashMap<>();
                Map map=new HashMap<>();
                map.put("code",0);
                map.put("msg","");
                map.put("data",map2);
                map2.put("src","/images/"+ dateStr+"/"+uuid+"." + prefix);
                return map;
            }

        }catch (Exception e){
        }finally{
            try {
                if(out!=null){
                    out.close();
                }
                if(fileInput!=null){
                    fileInput.close();
                }
            } catch (IOException e) {
            }
        }
        Map map=new HashMap<>();
        map.put("code",1);
        map.put("msg","");
        return map;

    }

tips:
1、后端代码就按照以前传统的图片上传代码来写就行,唯一区别的就是向前端传一个json串,json串里包括图片的url地址
Layui框架实现图片上传_第3张图片

这样,整个流程就完成了,其中最关键的就是后端代码了。
流程:图片先自己上传到后端
——》后端存储图片,并把存储地址给前端
——》前端的js代码获取到后端传来的图片存储地址,把这个url赋值给表单中的隐藏的图片输入项
——》图片输入框中有了地址,并随着表单其他内容一起提交~

效果展示:

前端页面:
Layui框架实现图片上传_第4张图片
数据库:
在这里插入图片描述

详细见官方文档:https://www.layui.com/doc/modules/upload.html

前面的demo内容预览

预览

登录界面

学生列表




班级列表

在这里插入图片描述



添加学生

在这里插入图片描述



我的考试成绩




各班级考试成绩

在这里插入图片描述



所有角色一览

在这里插入图片描述


各角色对应的权限

在这里插入图片描述



新建权限角色(可定制可观看的菜单和对应的操作按钮)




授予某个用户权限




你可能感兴趣的:(个人学习历程)