前端的js异步方法无法正常的执行下去,修改ueditor.all.js,大概在24568行
// domUtils.on(iframe, 'load', callback); // form.action = utils.formatUrl(imageActionUrl + (imageActionUrl.indexOf('?') == -1 ? '?':'&') + params); // form.submit();
换成下面代码
var formdata = new FormData(form);
var xhr= new XMLHttpRequest();
xhr.open("POST", me.getOpt('serverUrl')+'?action=uploadimage', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4)
if ((xhr.status >=200 && xhr.status < 300) || xhr.status == 304)
alert(xhr.responseText);
}
xhr.send(formdata);
xhr.onreadystatechange = function () {
if(xhr.readyState == 4) {
console.log(xhr.responseText);
var json = JSON.parse(xhr.responseText);
// alert(response.state);
if (json.state == 'SUCCESS' && json.url) {
loader = me.document.getElementById(loadingId);
loader.setAttribute('src', json.url);
loader.setAttribute('_src', json.url);
loader.setAttribute('title', json.title || '');
loader.setAttribute('alt', json.original || '');
loader.removeAttribute('id');
domUtils.removeClasses(loader, 'loadingclass');
} else {
showErrorLoader && showErrorLoader(json.state);
}
}
}
另外再附上后端的上传图片的接口
@RequestMapping("/ueditor")
@ResponseBody
public Object test(HttpServletRequest request,
@RequestParam(value = "action") String action,
@RequestParam(value = "upfile", required = false) MultipartFile file) throws Exception {
switch (action) {
case "config": // 加载返回ueditor配置文件conf/config.json
return ResourceUtils.getConfig();
case "uploadimage": // 上传图片
String state = "SUCCESS";
String uploadHttpUrl = fileUpload(file);
return resultMap(file, state, uploadHttpUrl);
case "uploadvideo": // 上传视频
String state2 = "SUCCESS";
String uploadHttpUrl2 = fileUpload(file);
return resultMap(file, state2, uploadHttpUrl2);
case "uploadfile": // 上传文件
String state3 = "SUCCESS";
String uploadHttpUrl3 = fileUpload(file);
return resultMap(file, state3, uploadHttpUrl3);
default:
return "无效action";
}
}
private Map<String, Object> resultMap(MultipartFile file, String state, String uploadHttpUrl) {
Map<String, Object> resMap = new HashMap<String, Object>();
resMap.put("state", state); //"SUCCESS" 表示成功
resMap.put("title", file.getOriginalFilename());
resMap.put("original", file.getOriginalFilename());
resMap.put("type", file.getContentType());
resMap.put("size", file.getSize());
resMap.put("url", uploadHttpUrl);
return resMap;
}