layui 富文本编辑器layedit 以及 图片转base64前端页面显示

 js

    var index = layui.layedit.build('noticeInformationContent', {
        area: ['500px', '400px'],
        uploadImage: {
            url: 'NI/uploadconimage' //接口url
            , type: 'POST' //默认post
        },
        hideTool: ['image']
    });

    layui.form.verify({
        content: function (val) {
            layui.layedit.sync(index);
            var content = layui.layedit.getContent(index);
            if (content.length > 1600) {
                return "内容不能超过1600字";
            }
        }
    })
    layui.form.render()

 java


    @PostMapping("/NI/uploadconimage")
    public Map uploadconimage(MultipartFile file) {
        Map result = new HashMap<>();
        try (BufferedInputStream in = new BufferedInputStream(file.getInputStream());
             ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            byte[] bytes = new byte[2 * 1024];
            int len;
            while ((len = in.read(bytes)) > 0) {
                out.write(bytes, 0, len);
            }
            byte[] byteArray = out.toByteArray();
            BASE64Encoder encoder = new BASE64Encoder();
            String encode = encoder.encode(byteArray);
            Map data = new HashMap<>();
            data.put("src", "data:image/jpg;base64," + encode);
            // data.put("src", "templetdata/static/images/banner.jpg");
            data.put("title", file.getOriginalFilename());
            result.put("code", 0);//重点:layui富文本不是code==200
            result.put("msg", "上传成功");
            result.put("data", data);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.put("code", -1);
            result.put("msg", "上传失败!");
        }
        return result;
    }

你可能感兴趣的:(前端,layui,javascript)