markdown 页面编辑器

markdown 页面编辑器

为了不影响读者疲劳,我长话短说。




个人主页:http://www.itit123.cn/ 更多干货等你来拿

效果图:


我重点讲解插入图片
直接点击插入图片,会提示 “upload_image_url not defined.” 的错误,看源码后得知要自己配置上传图片路径。在markdown-edit.js最后几行中有。
这是我填写的,仅供参考:
upload_image_url: '/sierew/blog/uploadImage',

在该js的315行,我习惯的添加了一个   enctype="multipart/form-data"
+ '<div class="span" style="width:300px"><form enctype="multipart/form-data">'

从408行到422行的代码中可以看出,插入图片需要我们返回一个json格式的数据,key有message,error,url值。
下面是上传代码:

@RequestMapping(value = "uploadImage" , method = RequestMethod.POST)
	@ResponseBody
	public String uploadImage(HttpServletRequest request,
			@RequestParam(value = "file" , required=false) MultipartFile file,
			@RequestParam(value = "text" , required=false) String fileName) {
		String realPath = request.getSession().getServletContext().getRealPath("/");
		String resourcePath = "static\\images\\uploadImages\\";
		String result = null;
		if(file!=null){
			if(GenerateUtils.allowUpload(file.getContentType())){
				// 存储本地
                File dir = new File(realPath + resourcePath);
                if(!dir.exists()){
                    dir.mkdirs();
                }
                File saveFile = new File(dir,file.getOriginalFilename());
                try {
					file.transferTo(saveFile);
					System.out.println("dir : " +dir);
					String callbackImgUrl = WEB_PATH + request.getSession().getServletContext().getContextPath() + "/" +resourcePath.replaceAll("\\\\","/") + file.getOriginalFilename();
					Map<String, Object> resultMap = new HashMap<String, Object>();
					resultMap.put("message", fileName);
					resultMap.put("url", callbackImgUrl);
					result = new Gson().toJson(resultMap);
				} catch (IllegalStateException e) {
					e.printStackTrace();
					result = "{\"error\":\"upload error\"}";
				} catch (IOException e) {
					e.printStackTrace();
					result = "{\"error\":\"upload error\"}";
				}
			}
		}
		System.out.println("result : " +result);
		return result;
	}

代码解释:
WEB_PATH是访问的路径,我的是"http://ip:port" 。用的是Gson 将map转json
Gson maven
<dependency>
		    <groupId>com.google.code.gson</groupId>
		    <artifactId>gson</artifactId>
		    <version>2.6.2</version>
		</dependency>

这样就可以了,看了几百行的js代码,换来了短短的几行代码,为了不让其他人走弯路,贡献自己的微薄之力。

本人菜鸟,有什么不对的地方请指出,我会及时更改。

遇到的问题:
开始没打算用gson,打算自己拼接json格式的字符串,结果一直失败提示:SyntaxError: JSON.parse: unexpected character at line 6 column 1 of the JSON dat ,说什么我不按照格式来。
修改了格式后提示:SyntaxError: JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data , 百度后发现是jquery解析json的时候,不识别单引号。 最后用gson一次成功。


如果你的项目中用到了sitemesh , 建议将该页面屏蔽。

上传图片的效果图:





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值