ckeditor实现图片上传

之前使用ckeditor+ckfinder实现的文件上传功能,由于现阶段应用场景ckfinder不适用,所以专门查阅文档实现单一ckeditor

的文件上传功能,以方便后面再次使用

一.使用的是ckeditor3.3.5版本

第一步:打开ckeditor/plugins/image/dialogs/image.js,搜索hidden,将所有出现id:"Upload",hidden:true的true改为false

第二步:完成上传Controller

@RequestMapping(value = "/uploadFile2")
	public void uploadPic(@RequestParam(value = "upload", required = false) MultipartFile file,
						  HttpServletResponse response,String CKEditorFuncNum) throws IOException {
		PrintWriter out =response.getWriter();
		response.setCharacterEncoding("utf-8");
		String imageName = file.getOriginalFilename();
		String expandedName = "";
		String uploadContentType  = file.getContentType();
		InputStream is;
		String filepath;
		StringBuilder sb = new StringBuilder();

			is = file.getInputStream();
			Long size = file.getSize();
			if (uploadContentType.equals("image/pjpeg")
					|| uploadContentType.equals("image/jpeg")) {
				// IE6上传jpg图片的headimageContentType是image/pjpeg,而IE9以及火狐上传的jpg图片是image/jpeg
				expandedName = ".jpg";
			} else if (uploadContentType.equals("image/png")
					|| uploadContentType.equals("image/x-png")) {
				// IE6上传的png图片的headimageContentType是"image/x-png"
				expandedName = ".png";
			} else if (uploadContentType.equals("image/gif")) {
				expandedName = ".gif";
			} else if (uploadContentType.equals("image/bmp")) {
				expandedName = ".bmp";
			} else {
				sb.append("<script type=\"text/javascript\">");
				sb.append("window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum
						+ ",''," + "'文件格式不正确(必须为.jpg/.gif/.bmp/.png文件)');");
				sb.append("</script>");
				out.println(sb.toString());
				return ;
			}
System.out.println("imageName: " + imageName + "   uploadContentType: " + uploadContentType);
			if (size > 1024 * 1024) {
				sb.append("<script type=\"text/javascript\">");
				sb.append("window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum
						+ ",''," + "'文件大小不得大于600k');");
				sb.append("</script>");
				out.println(sb.toString());
				return ;
			}
			DateFormat df = new SimpleDateFormat();
			imageName = df.format(new Date()) + expandedName;

			//上传文件到资源服务器
			filepath = ImageUpload.uploadStreamImg(ip, is, size, imageName);
			//filepath = path + filepath.substring(filepath.indexOf("/", 10));
			sb.append("<script type=\"text/javascript\">");
			sb.append("window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum
					+ ",'"  + filepath + "','')");
			sb.append("</script>");
			System.out.println("filePath: " + sb.toString());

			out.println(sb.toString());
			return ;

		//return filepath;
	}

第三步:配置点击” 上传到服务器“ 按钮之后请求的控制器,在ckeditor/config.js中加入这一段代码

config.image_previewText = '';
config.filebrowserImageUploadUrl = "/uploadFile2";

如果 image_previewText 配置文件不生效,可以去ckeditor/plugins/image/dialogs/image.js 文件ckeditor/plugins/image/dialogs/image.js

文件搜索image_previewText 删除文本框默认内容

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值