文件上传SimpleUploadAdapter
2020-06-24 本文已影响0人
sen_coder
增加组件
- 进入ckeditor 仓库
cd ckeditor5-build-classic
npm install --save @ckeditor/ckeditor5-upload
- 修改src/ckeditor.js
import ImageCaption from '@ckeditor/ckeditor5-image/src/imagecaption';
import ImageStyle from '@ckeditor/ckeditor5-image/src/imagestyle';
import ImageToolbar from '@ckeditor/ckeditor5-image/src/imagetoolbar';
import ImageUpload from '@ckeditor/ckeditor5-image/src/imageupload';
import Indent from '@ckeditor/ckeditor5-indent/src/indent';
import Link from '@ckeditor/ckeditor5-link/src/link';
import List from '@ckeditor/ckeditor5-list/src/list';
import MediaEmbed from '@ckeditor/ckeditor5-media-embed/src/mediaembed';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import PasteFromOffice from '@ckeditor/ckeditor5-paste-from-office/src/pastefromoffice';
import Table from '@ckeditor/ckeditor5-table/src/table';
import TableToolbar from '@ckeditor/ckeditor5-table/src/tabletoolbar';
import TextTransformation from '@ckeditor/ckeditor5-typing/src/texttransformation';
import Alignment from '@ckeditor/ckeditor5-alignment/src/alignment';
import ImageResize from '@ckeditor/ckeditor5-image/src/imageresize';
import SimpleUploadAdapter from '@ckeditor/ckeditor5-upload/src/adapters/simpleuploadadapter';
export default class ClassicEditor extends ClassicEditorBase {}
// Plugins to include in the build.
ClassicEditor.builtinPlugins = [
Essentials,
UploadAdapter,
SimpleUploadAdapter,
Autoformat,
Bold,
Italic,
- 重新build
npm run build
将 build/ckeditor.js复制到对应的项目中.
前端代码
- 导入js
<script src="${pageContext.request.contextPath}/scripts/ckeditor5/ckeditor.js"></script>
- 创建Editor对象
<script>
ClassicEditor
.create( document.querySelector( '#editor' ), {
plugins: [ SimpleUploadAdapter, ... ],
toolbar: [ ... ],
simpleUpload: {
uploadUrl: 'http://example.com',
// Headers sent along with the XMLHttpRequest to the upload server.
headers: {
'X-CSRF-TOKEN': 'CSFR-Token',
Authorization: 'Bearer <JSON Web Token>'
}
}
} )
.then( editor=>{
edit = editor;
editor.plugins.get('FileRepository').createUploadAdapter = (loader)=>{
return new UploadAdapter(loader);
};
} )
.catch( ... );
</script>
- uploadAdapter对象
class UploadAdapter {
constructor(loader) {
this.loader = loader;
}
upload() {
return new Promise((resolve, reject) => {
const data = new FormData();
data.append('upload', this.loader.file);
data.append('allowSize', 10);//允许图片上传的大小/兆
$.ajax({
url: '你的上传路径',
type: 'POST',
data: data,
dataType: 'json',
processData: false,
contentType: false,
success: function (data) {
if (data.res) {
resolve({
alert(data.url);
default: data.url
});
} else {
reject(data.msg);
}
}
});
});
}
abort() {
}
}