当前位置:网站首页>UEditor圖片跨域上傳解决方案
UEditor圖片跨域上傳解决方案
2022-06-09 16:26:00 【木魚大叔】
預設環境
- UEditor版本:1.4
- 客戶端地址:http://www.aaa.com
- 圖片服務器地址:http://www.bbb.com
- 圖片服務器端UEditor的部署URL:http://www.bbb.com/ueditor/
- 客戶端和圖片服務器各部署一套UEditor環境
客戶端修改
修改1:/ueditor/ueditor.config.js
將原有的serverUrl注釋掉,然後在下面新增一行,內容為
, serverUrl: "http://www.bbb.com/ueditor/php/controller.php"如圖:

修改2:/ueditor/php/config.json
修改文件訪問路徑前綴,具體為,將該文件中所有以UrlPrefix結尾的參數,修改為http://www.bbb.com
如圖:

修改3:增加回調函數
在http://www.aaa.com根目錄下,新建一個ue_upload_callback.php文件,內容如下:
$ueUploadCallback = new UeUploadCallback();
call_user_func_array(array($ueUploadCallback,'index'),array());
class UeUploadCallback {
public function index() {
die($_REQUEST['ueResult']);
}
}圖片服務器端修改
服務器端,只需要修改/ueditor/php/controller.php一個文件即可。主要就是增加一個callbackUrl參數,用於跨域時的數據回調,同時增加跨域權限設置。
1. 增加跨域權限設置
header('Access-Control-Allow-Origin:http://www.aaa.com');
header('Access-Control-Allow-Methods:POST,GET,OPTIONS');
header('Access-Control-Allow-Credentials:true');
$requestMethod = $_SERVER['REQUEST_METHOD'];
if ($requestMethod == 'OPTIONS') {
header('Content-Type:text/plain charset=UTF-8');
header('Content-Length: 0',true);
header('status: 204');
header('HTTP/1.0 204 No Content');
} else {
header('Access-Control-Allow-Headers:Origin,X-Requested-With,X_Requested_With,Content-Type,Accept');
}2. 增加跨域回調參數
將原有的代碼
/* 輸出結果 */
if (isset($_GET["callback"])) {
if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
} else {
echo json_encode(array(
'state'=> 'callback參數不合法'
));
}
} else {
echo $result;
}修改為
/* 輸出結果 */
if (isset($_GET["callback"])) {
if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
} else {
echo json_encode(array(
'state'=> 'callback參數不合法'
));
}
} else {
if ($_REQUEST['callbackUrl']) {
header("Location:".$_REQUEST['callbackUrl']."?ueResult=".urlencode($result));
} else {
echo $result;
}
}注意:此處的ueResult參數,客戶端將通過該參數,獲取UEditor返回的數據。
客戶端調用
<script id="container" name="content" type="text/plain"></script>
<script type="text/javascript">
var ue = UE.getEditor('container');
ue.ready(function() {
//綁定自定義的回調URL
ue.execCommand('serverparam', {
'callbackUrl': 'http://www.aaa.com/ue_upload_callback.php'
});
});
</script>
边栏推荐
- Nth child selector
- 第一章 ESQL介绍
- An alternative to enumeration -- Enumeration classes
- FFmpeg思维导图
- What is a microservice architecture?
- Golang reflect: code examples for reflecting (obtaining values) basic data types and struct structures
- Rapid development template based on ABP vNext
- R language plot visualization: use the plot visualization model to create a grid by using the classification contour line (contour line) of the entire data space and meshgrid, where the distance betwe
- 软件测试面试必问的10个问题
- Laravel8框架七牛雲上傳
猜你喜欢

The end of an era! Wu Enda's classic machine learning course was closed this month however

面试官问我一条update语句加了多少锁?我总结了全套八股文

GoLand运行go程序时working directory的设置问题:报错路径找不到no such file or directory时需检查该配置

After the change of beauty brands and consumer demand, how can retailers not fall behind?

看完就会,从抓包到接口测试的全过程解析

Under the epidemic situation, four points of cognition help you grow!

Dapr 源码解析 | 基本介绍

Windows下安装Redis及php的redis扩展

Reconstruction essentials learning

FFmpeg思维导图
随机推荐
Jpex launches bayc mayc contract series welcome experience
Differences between single application and microservice invocation
[SEETF]Super Secure Requests Forwarder
渗透测试-后渗透+lcx+frp
Laravel8框架七牛云上传
Easywechat realizes wechat real payment operation
若依 思维导图
关闭StackExchange等平台的privacy收集窗口
Are cash financial products breakeven?
Ruoyi mind map
基于 abp vNext 的快速开发模板
现金类的理财产品是保本的吗?
Where to open an account for futures and who to open it for safe and reliable??
Consul学习
【GO语言学习之路】结构体实战
Usage of dac8560
WordPress网站底部的自定页面(如:网站地图等)按钮美化教程
flutter系列之:Material主题的基础-MaterialApp
还在从零开始搭建项目?这款升级版快速开发脚手架值得一试~
办公桌子和椅子的选择和注意点