Ueditor 跨域上传文件问题

本文详细介绍了如何修改Ueditor单文件上传的DOMUtils代码,使其能够支持跨域上传,并提供了XMLHttpRequest跨域解决方案及SpringMvc配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ueditor官网上明确指出单文件上传不支持跨域,请小伙伴发挥想象力!

看了下ueditor.all.js源码,上传单文件使用的是iframe实现的。不能跨域。于是改了这部分

domUtils.on(input, 'change', function()这个地方的代码。这地方是上传文件后执行的代码。把iframe那套改下就行。
var fd = new FormData();
fd.append("fileToUpload", input.files[0]);

var xhr = false;
try{
	xhr=new XMLHttpRequest();// 创建 XMLHttpRequest对象,IE可能不行
}catch(e){	  
	xhr=ActiveXobject("Msxml12.XMLHTTP");// FOR IE
}

xhr.onreadystatechange = function(e) {
	  if (xhr.readyState == 4) {
		  if (xhr.status == 200) {
			var responseText = xhr.responseText;
			json = (new Function("return " + responseText))();
			link = "http://static.xxxxx.com/fileupload" + json.filesDetail[0].savePath;
			if(json.success && json.filesDetail[0].savePath) {
				loader = me.document.getElementById(loadingId);
				loader.setAttribute('src', link);
				loader.setAttribute('_src', link);
				loader.setAttribute('title', json.filesDetail[0].saveFile || '');
				loader.setAttribute('alt', json.filesDetail[0].uploadName || '');
				loader.removeAttribute('id');
				domUtils.removeClasses(loader, 'loadingclass');
			} else {
				showErrorLoader && showErrorLoader(json.failReason);
			}
		  } else {
			  showErrorLoader && showErrorLoader("上传失败");		
		  }
		  form.reset();
	  }
  };
xhr.open("POST", "http://static.xxxxxx.cc/file/upload");
xhr.send(fd);
//这种修改是直接把上传的连接和返回的json格式直接写死了(我们用的上传项目返回的json格式与ueditor不一样)。这个仅供参考,建议上传的url可以用重写getActionUrl方法实现(忘了叫什么名字,那个方法),地址前缀在ueditor的配置文件中配置即可。

XMLHttpRequest 新版本的是支持跨域的。不过这个需要你服务器端做些配置。例如springMvc需要在web.xml中org.springframework.web.servlet.DispatcherServlet加入

<init-param>  
    <param-name>dispatchOptionsRequest</param-name>  
    <param-value>true</param-value>  
</init-param>

详情可以参考http://my.oschina.net/northerSong/blog/473227


转载于:https://my.oschina.net/northerSong/blog/523908

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值