[英]Can't upload image in ckeditor
I am using CKEDITOR5 and would like to be able to allow users to upload images.我正在使用 CKEDITOR5 并希望能够允许用户上传图像。 I am using customized upload adaptor described on the official website but I can't use XMLHttpRequest to upload images.
我正在使用官方网站上描述的自定义上传适配器,但我无法使用 XMLHttpRequest 上传图像。 Everytime I upload image it gives the following error: Access to XMLHttpRequest at 'https://xxxxxx' from origin 'http://xx.xx.xx.xx:xxxx' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
每次我上传图片时,它都会出现以下错误: CORS 策略已阻止来自原点“http://xx.xx.xx.xx:xxxx”的“https://xxxxxx”处的 XMLHttpRequest 访问:对预检请求的响应不'不通过访问控制检查:请求的资源上不存在'Access-Control-Allow-Origin' header。 I pass the image to backend and upload it to s3 successfully, backend returns a correct s3 url, and I use it as the url of xhr.open('POST', url, true) .
我将图像传递到后端并成功将其上传到s3,后端返回正确的s3 url,我将其用作xhr.open('truePOST',Z572D4E421E5E6B7112A的url)的Z572D4E421E8A027112Z 。 Here is the code of my customized adaptor:
这是我的自定义适配器的代码:
import axios from 'axios'
import { postAccessHeader, clearLocalStorage } from '../../common/publicFunc';
class MyUploadAdapter {
constructor( loader ) {
this.loader = loader;
this.url = '';
}
upload() {
return this.loader.file
.then( file => new Promise( ( resolve, reject ) => {
this._s3_upload_image( resolve, reject, file );
} ) );
}
abort() {
if ( this.xhr ) {
this.xhr.abort();
}
}
_initRequest( url ) {
const xhr = this.xhr = new XMLHttpRequest();
xhr.open( 'POST', url, true );
xhr.responseType = 'json';
}
_initListeners( resolve, reject, file ) {
const xhr = this.xhr;
const loader = this.loader;
const genericErrorText = `Couldn't upload file: ${ file.name }.`;
xhr.addEventListener( 'error', () => reject( genericErrorText ) );
xhr.addEventListener( 'abort', () => reject() );
xhr.addEventListener( 'load', () => {
const response = xhr.response;
if ( !response || response.error ) {
return reject( response && response.error ? response.error.message : genericErrorText );
}
resolve( {
default: response.url
} );
} );
if ( xhr.upload ) {
xhr.upload.addEventListener( 'progress', evt => {
if ( evt.lengthComputable ) {
loader.uploadTotal = evt.total;
loader.uploaded = evt.loaded;
}
} );
}
}
_sendRequest( file ) {
const data = new FormData();
data.append( 'upload', file );
this.xhr.send( data );
}
_s3_upload_image( resolve, reject, file ){
console.log(file)
const formData = new FormData();
formData.append( 'upload', file);
let payload = {"image": formData, "file": file, "test": "hi"}
console.log(payload)
let settings = postAccessHeader('/account/s3_upload_image', formData);
axios(settings)
.then(response => {
const code = response.status;
console.log(response)
if (code == 200) {
if (response.data.status) {
let url = response.data.url
this._initRequest( url );
this._initListeners( resolve, reject, file );
this._sendRequest( file );
} else {
if (response.data.AuthFailed) {
clearLocalStorage();
}
else {
}
}
} else {
alert("unable to upload image");
}
})
.catch(error => {
alert(error);
});
}
}
export default MyUploadAdapter
Thanks for any help!谢谢你的帮助!
I have been through this a week ago.我一周前经历过这个。 I suggest using CKEditor4 This working on my web.
我建议在我的 web 上使用 CKEditor4。 It will auto-embed video and images from URL
它将自动嵌入来自 URL 的视频和图像
Use npm i ckeditor4-react
to install ckeditor4-react使用
npm i ckeditor4-react
安装ckeditor4-react
import CKEditor from "ckeditor4-react";
import React from "react";
export default function editor() {
const [value,setValue]=useState('')
return (
<div>
<CKEditor
data={value}
config={{
embed_provider:
"//ckeditor.iframe.ly/api/oembed?url={url}&callback={callback}",
extraPlugins: ["embed", "autoembed"],
mediaEmbed: { previewsInData: true, autoEmbed: true },
}}
onChange={(e) => {
let data = e.editor.getData();
setValue(data);
}}
/>
</div>
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.