您的位置:首页 > 其它

kindeditor的跨域上传文件

2015-07-15 12:26 267 查看
目前只支持在同一根域下的支持,如从(a.test.com上传到img.test.com)网上例子很多,但是太乱了,其实很简单,几行代码就解决了,下面请看:首先,去官网下载最新的kindeditor,然后部署到img.test.com上,保证可以通过img.test.com/jsp/upload_json.jsp 访问到kindeditor的接口然后,修改img服务器上的upload_json.jsp,在顶部添加
/**
 * KindEditor JSP
 * 
 * 本JSP程序是演示程序,建议不要直接在实际项目中使用。
 * 如果您确定直接使用本程序,使用之前请仔细确认相关安全设置。
 * 
 */
//文件保存目录路径
String savePath = pageContext.getServletContext().getRealPath("/") + "attached/";
//文件保存目录URL
//String uri  = request.getContextPath() + "/attached/";
String saveUrl  = "http://img.test.com/attached/";
接着在底下out.println(obj.toJSONString());的上面添加
out.println("<script type='text/javascript'>document.domain = 'ezhensuo.com'</script>");
这样,服务器端的配置就ok了然后,在本地部署kindedior,jsp上初始化kindeditor
<script type="text/javascript">
document.domain = "test.com";
KindEditor.ready(function(K) {
	var editor1 = K.create('textarea[name="contentText"]', {
		afterBlur: function () { this.sync(); },
		cssPath : 'http://file.ezhensuo.com/plugins/code/prettify.css',
		uploadJson : 'http://file.ezhensuo.com/jsp/upload_json.jsp',
		fileManagerJson : 'http://file.ezhensuo.com/jsp/file_manager_json.jsp',
		allowFileManager : true,
		allowImageRemote:false,
		items:[
			'source', '|', 'undo', 'redo', '|', 'preview',  'cut', 'copy', 'paste',
			'plainpaste',  '|', 'justifyleft', 'justifycenter', 'justifyright',
			'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
			'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/',
			'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
			'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image', 
			 'table', 'hr', 'emoticons', 'pagebreak',
			'anchor', 'link', 'unlink', '|'
		],
		afterCreate : function() {
			var self = this;
			K.ctrl(document, 13, function() {
				self.sync();
				document.forms['example'].submit();
			});
			K.ctrl(self.edit.doc, 13, function() {
				self.sync();
				document.forms['example'].submit();
			});
		}
	});
	prettyPrint();
});
</script>
这样,跨域就完成了,其实最主要的就是document.domain = "test.com";//根域因为,编辑器原理大多是Form提交到iframe,然后本地通过js调用返回的url,但是你不写domain的话就会报域名头不一致无法跨域的问题,现在服务器端返回一个script domain就可以跨域了哈
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: