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加入
dispatchOptionsRequest
true