项目通常引入的是kindeditor-all.min.js,这里我们需要改为引入kindeditor-all.js,因为要对其源码进行修改。
1.打开文件,搜索下面的这行代码:
KindEditor.plugin('image', function(K) {
2.查找下面提交图片办法,并将其注释掉,因为会出现跨域问题:
//uploadbutton.submit();
3.把下边的代码粘在上一行代码的后边:
var formData = newFormData();var file=uploadbutton.fileBox[0].files[0];
formData.append(file.name, file);//console.log(file,formData)
K.ajaxForm(self.options.uploadJson, function(data) {
dialog.hideLoading();//console.log(data);
if (data.error==0) {//console.log(self.options);
var html = '';//console.log(html)
self.appendHtml(html).hideDialog().focus();
}
},'POST',formData,'json');
4.输入_ajax,查找_ajax名称的函数,在此函数的后面新增如下代码:
function_ajaxForm(url, fn, method, param, dataType) {
method= method || 'GET';
dataType= dataType || 'json';var xhr = window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
xhr.open(method, url,true);
xhr.onreadystatechange= function() {if (xhr.readyState == 4 && xhr.status == 200) {if(fn) {var data =_trim(xhr.responseText);if (dataType == 'json') {
data=_json(data);
}
fn(data);
}
}
};
xhr.send(param);
}
5.在此函数后面加上如下代码,这样就可以用ajax方式上传图片了:
K.ajaxForm=_ajaxForm;
tip:本文是为了遇到类似问题时方便本人查找
原文链接:/alongxiao/java/article/details/104831596