以下代码是在tinymce中译文档中的代码
tinymce.init({
selector: '.tinymce', language: 'zh_CN',
mobile: {
plugins: [
'a11ychecker', 'advlist', 'advcode', 'advtable', 'autolink', 'checklist', 'export',
'lists', 'link', 'image', 'charmap', 'preview', 'anchor', 'searchreplace', 'visualblocks',
'powerpaste', 'fullscreen', 'formatpainter', 'insertdatetime', 'media', 'table', 'help', 'toc', 'wordcount'
],
toolbar:['undo redo | formatpainter casechange blocks table ' ,
'| bold italic backcolor | alignleft aligncenter alignright alignjustify code ',
'bullist numlist checklist outdent indent | removeformat | a11ycheck help'],
images_upload_handler: (blobInfo, success, failure) => {
var xhr, formData;
var file = blobInfo.blob();//转化为易于理解的file对象
xhr = new XMLHttpRequest();
xhr.withCredentials = false;
xhr.open('POST', '/xxxxx');
xhr.onload = function() {
var json=JSON.parse(xhr.responseText)
if (json.code===200){success(json.data)}
else {failure(json.message)}
};
formData = new FormData();
formData.append('file', file, file.name );//此处与源文档不一样
xhr.send(formData);
}
}
});
文件上传效果如下,实际上请求已经成功,说明不是后端代码问题
这是官方原文档,中译估计是没更新,官方已经更新了6.0的自定义写法,示例代码如下:
const example_image_upload_handler = (blobInfo, progress) => new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.withCredentials = false;
xhr.open('POST', 'postAcceptor.php');
xhr.upload.onprogress = (e) => {
progress(e.loaded / e.total * 100);
};
xhr.onload = () => {
if (xhr.status === 403) {
reject({ message: 'HTTP Error: ' + xhr.status, remove: true });
return;
}
if (xhr.status < 200 || xhr.status >= 300) {
reject('HTTP Error: ' + xhr.status);
return;
}
const json = JSON.parse(xhr.responseText);
if (!json || typeof json.location != 'string') {
reject('Invalid JSON: ' + xhr.responseText);
return;
}
resolve(json.location);
};
xhr.onerror = () => {
reject('Image upload failed due to a XHR Transport error. Code: ' + xhr.status);
};
const formData = new FormData();
formData.append('file', blobInfo.blob(), blobInfo.filename());
xhr.send(formData);
});
tinymce.init({
selector: 'textarea', // change this value according to your HTML
images_upload_handler: example_image_upload_handler
});