wangEditor3 上传本地视频和图片

一、js下载地址

https://download.csdn.net/download/i_am_yong_ge/11633651

二、前端:

引用wangEditor.js

var E = window.wangEditor
var editor = new E('#editor')
//开启配置
editor.customConfig.debug = true
// 隐藏“网络图片”tab
editor.customConfig.showLinkImg = true
// 关闭粘贴内容中的样式
editor.customConfig.pasteFilterStyle = false
// 忽略粘贴内容中的图片
editor.customConfig.pasteIgnoreImg = false
// 将图片大小限制为 3M
editor.customConfig.uploadImgMaxSize = 5 * 1024 * 1024
// 限制一次最多上传 1 张图片
editor.customConfig.uploadImgMaxLength = 1
editor.customConfig.uploadImgServer = 'uploadImg'
editor.customConfig.uploadFileName = 'myFileName'

// 使用 base64 保存图片
editor.customConfig.uploadImgShowBase64 = true

editor.customConfig.uploadImgHooks = {
	customInsert: function (insertImg, result, editor) {
		var url =result.data;//获取后台返回的url
		console.log("image url = " + url)
		insertImg(url);
	}
};

//上传视频
editor.customConfig.uploadVideoServer = 'uploadVideo'

editor.customConfig.uploadVideoHooks = {
	customInsert: function (insertImg, result, editor) {
		var url =result.data;//获取后台返回的url
		console.log("video url = " + url)
		insertImg(url);
	}
};
editor.create();

三、后端

	@RequestMapping(value = "/uploadImg")
	@ResponseBody
	public Map<String, String> uploadImg(
			@RequestParam(value = "myFileName") MultipartFile file,
			HttpServletRequest request) {
		try {
			String type = file.getContentType();
			String uploadDir = request.getSession().getServletContext().getRealPath("/") + "upload\\photo\\";
			String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
			String curDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
			String fileName = curDate + "\\" + System.currentTimeMillis() + suffix;
			String path = uploadDir + fileName;
			File myfile = new File(path);
			FileUtils.createDirectory(myfile);
			file.transferTo(myfile);

			String separator = System.getProperty("file.separator");
			String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath()
					+ separator + "upload" + separator + "photo" + separator;

			String str = basePath + fileName;
			Map<String, String> map = new HashMap<String, String>();
			map.put("data", str);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}


	@PostMapping("/uploadVideo")
	@ResponseBody
	public synchronized Map uploadVideo(MultipartHttpServletRequest request) throws IOException {
		try {
			MultipartFile file = request.getFile("file");

			String type = file.getContentType();
			String uploadDir = request.getSession().getServletContext().getRealPath("/") + "upload\\video\\";
			String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
			String curDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
			String fileName = curDate + "\\" + System.currentTimeMillis() + suffix;
			String path = uploadDir + fileName;
			File myfile = new File(path);

			FileUtils.createDirectory(myfile);
			file.transferTo(myfile);
			
			String separator = System.getProperty("file.separator");
			String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath()
					+ separator + "upload" + separator + "video" + separator;
			String str = basePath + fileName;
			
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("data",str);
			map.put("errno",0);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

四、效果预览

image

image

五、注意

上传后,可能发现一切正常,但是没有图片
很有可能是没有编辑器还没有光标

下载积分快用完了,帮忙支持下哈
JS下载地址:https://download.csdn.net/download/i_am_yong_ge/11633651

六、参考资料:

https://blog.csdn.net/one_hwx/article/details/87652525

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
WangEditor是一款基于JavaScript的富文本编辑器,它允许用户在网页上进行文本编辑,并且提供了上传本地图片的功能。 在使用WangEditor上传本地图片并回填的过中,需要执行以下步骤: 1. 添加WangEditor的引用:在网页的<head>标签中添加WangEditor的CSS和JavaScript文件的引用。 2. 创建编辑器实例:在网页中创建一个<div>元素,并且给它一个唯一的id作为标识。然后使用JavaScript代码实例化WangEditor编辑器,将该<div>元素作为参数入。 3. 配置图片上传:使用WangEditor提供的配置项,设置图片上传的相关参数,包括图片上传的URL、上传方式、上传图片的大小限制等。也可以自定义图片上传的返回结果。 4. 编辑器中上传图片:在编辑器的工具栏中添加一个上传图片的按钮,并且为该按钮绑定事件处理函数。在事件处理函数中,调用WangEditor上传图片的方法,打开文件选择对话框,选择本地的图片文件。 5. 图片上传成功:当用户选择了本地图片文件后,WangEditor会将选择的图片文件上传到服务器,并获取到服务器返回的图片URL。在图片上传成功后,可以使用JavaScript获取到编辑器实例,并通过设置编辑器内容的方式,在编辑器中插入该图片。 总结起来,WangEditor提供了一套完善的API和配置项,可以方便地实现上传本地图片并回填的功能。用户只需要按照上述步骤进行操作,即可在网页中使用WangEditor上传本地图片并回填。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值