解决layui富文本编辑器图片上传无法回显的问题

layui富文本编辑器用起来挺方便的,但是不足的是不提供图片上传的接口,需要自己写上传接口,而且返回的数据类型要符合layui富文本编辑器图片上传插件的要求,否则图片可以上传成功,但是无法回显,这个问题找了好久才找到原来是返回的数据结构不符合layui要求,经过修改才得以解决,现在把代码贴出来共享。加粗字体的代码是返回数据结构

@ResponseBody @RequestMapping(value = "fillupf", method = RequestMethod.POST) public String fillupf(@RequestParam("file") MultipartFile[] files) { try { String[] courseware = new String[files.length]; int index = 0; for (MultipartFile file : files) { boolean isLegal = false; for (String type : ALLOW_FILE_TYPE) { if (StringUtils.endsWithIgnoreCase(file.getOriginalFilename(), type)) { isLegal = true; break; } } // 封装Result对象,并且将文件的byte数组放置到result对象中 PicUploadResult fileUploadResult = new PicUploadResult(); // 状态 fileUploadResult.setError(isLegal ? 0 : 1); // 文件新路径 String filePath = getFilePath(file.getOriginalFilename()); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Pic file upload .[{}] to [{}] .", file.getOriginalFilename(), filePath); } // 生成图片的绝对引用地址 String picUrl = StringUtils.replace(StringUtils.substringAfter(filePath, propertieService.REPOSITORY_PATH), "\\", "https://www.jb51.net/"); fileUploadResult.setUrl(propertieService.IMAGE_BASE_URL + picUrl); File newFile = new File(filePath); // 写文件到磁盘 file.transferTo(newFile); // 状态 fileUploadResult.setError(isLegal ? 0 : 1); if (!isLegal) { // 不合法,将磁盘上的文件删除 newFile.delete(); } courseware[index++] = fileUploadResult.getUrl(); } String fillUrls=StringUtils.join(courseware, ","); Map<String,Object> map = new HashMap<String,Object>(); Map<String,Object> map2 = new HashMap<String,Object>(); map.put("code",0);//0表示成功,1失败 map.put("msg","上传成功");//提示消息 map.put("data",map2); map2.put("src",fillUrls);//图片url map2.put("title","图片丢失");//图片名称,这个会显示在输入框里 JSONObject jsonObject = JSONObject.fromObject(map); String result = jsonObject.toString(); return result; } catch(Exception e) { } return null; }

以上这篇解决layui富文本编辑器图片上传无法回显的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/f1299e43bb4b50b2f16d497c09ca1881.html