您的位置:首页 > 产品设计 > UI/UE

使用ueditor小结

2017-11-04 14:58 246 查看
前端html页面
<script id="editor" name="content" type="text/plain" style="width:960px;height:500px;float:left;margin-top:30px;">$detail['content']</script>

<script>
//实例化编辑器 红色内容为修改的配置

var ue = UE.getEditor('editor',{serverUrl:'<?=Url::to(['teacher/edit-upload-v1'])?>',allowDivTransToP:false});

</script>

后端
后端图片上传调用方法 TeacherController.php
/**
* UEDITOR图片上传功能接口
* @return array
*/
public function actionEditUploadV1()
{
$up = new UeditorUploadV1();
$result = $up->index();
return $result;
}

图片上传处理
UeditorUploadV1.php

public function index()
{
$action = $_GET['action'];
switch ($action) {
case 'config':
$config = $this->ueditor_config;
$config['uid'] = intval($_GET['uid']);
$result =  json_encode($config);
break;

/* 上传图片 */
case 'uploadimage':
/* 上传涂鸦 */
case 'uploadscrawl':
/* 上传视频 */
case 'uploadvideo':
/* 上传文件 */
case 'uploadfile':
$this->upload($action);
$result = json_encode($this->getFileInfo());
break;
}
}

/**
* 获取当前上传成功文件的各项信息
* @return array
*/
public function getFileInfo()
{
return array(
"state" => $this->stateInfo,
"url" => str_replace(Yii::$app->params['cdnpublicpath'],Yii::$app->params['cdnpublicurl'],$this->filePath),
"title" => $this->fileName,
"original" => $this->oriName,
"type" => $this->fileType,
"size" => $this->fileSize
);
}
/**
* 表单配置
* @param string $fileField 表单名称
* @param array $config 配置项
* @param bool $base64 是否解析base64编码,可省略。若开启,则$fileField代表的是base64编码的字符串表单名
*/
public function upload($action)
{
$type = "upload";
switch($action){
case 'uploadimage':  //匹配为图片
$config = array(
"pathFormat" => $this->ueditor_config['imagePathFormat'],
"maxSize" => $this->ueditor_config['imageMaxSize'],
"allowFiles" => $this->ueditor_config['imageAllowFiles']
);
break;
//其他文件类型  同上。。。
}
$this->upFile();
$this->stateMap['ERROR_TYPE_NOT_ALLOWED'] = iconv('unicode', 'utf-8', $this->stateMap['ERROR_TYPE_NOT_ALLOWED']);
}

/**
* 上传文件的主处理方法
* @return mixed
*/
private function upFile()
{
$file = $this->file = $_FILES[$this->fileField];
if (!$file) {
$this->stateInfo = $this->getStateInfo("ERROR_FILE_NOT_FOUND");
return;
}
if ($this->file['error']) {
$this->stateInfo = $this->getStateInfo($file['error']);
return;
} else if (!file_exists($file['tmp_name'])) {
$this->stateInfo = $this->getStateInfo("ERROR_TMP_FILE_NOT_FOUND");
return;
} else if (!is_uploaded_file($file['tmp_name'])) {
$this->stateInfo = $this->getStateInfo("ERROR_TMPFILE");
return;
}

//上传的文件信息
$this->oriName = $file['name'];
$this->fileSize = $file['size'];
$this->imgType = exif_imagetype($file['tmp_name']);
$this->fileType = $this->getFileExt();
$this->fullName = $this->getFullName();
$this->filePath = $this->getFilePath();
$this->fileName = $this->getFileName();
$dirname = dirname($this->filePath);

//检查文件大小是否超出限制
if (!$this->checkSize()) {
$this->stateInfo = $this->getStateInfo("ERROR_SIZE_EXCEED");
return;
}
//检查是否不允许的文件格式
if (!$this->checkType()) {
$this->stateInfo = $this->getStateInfo("ERROR_TYPE_NOT_ALLOWED");
return;
}
//创建目录失败
if (!file_exists($dirname) && !mkdir($dirname, 0777, true)) {
$this->stateInfo = $this->getStateInfo("ERROR_CREATE_DIR");
return;
} else if (!is_writeable($dirname)) {
$this->stateInfo = $this->getStateInfo("ERROR_DIR_NOT_WRITEABLE");
return;
}
//移动文件
if (!(move_uploaded_file($file["tmp_name"], $this->filePath) && file_exists($this->filePath))) { //移动失败
$this->stateInfo = $this->getStateInfo("ERROR_FILE_MOVE");
} else { //移动成功
$this->stateInfo = $this->stateMap[0];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ueditor 前端