前端代碼
<script type="text/javascript" src="/wangEditor.min.js"></script>
<script type="text/javascript">
var E = window.wangEditor
var editor = new E('#editor')
// 或者 var editor = new E( document.getElementById('editor') )
editor.customConfig.uploadImgServer = '/upload/index'
editor.customConfig.uploadFileName = 'image'
editor.customConfig.menus = [
'head', // 標(biāo)題
'bold', // 粗體
'fontSize', // 字號(hào)
'fontName', // 字體
'italic', // 斜體
'underline', // 下劃線
'strikeThrough', // 刪除線
'foreColor', // 文字顏色
'backColor', // 背景顏色
'link', // 插入鏈接
'list', // 列表
'justify', // 對(duì)齊方式
'emoticon', // 表情
'image', // 插入圖片
'table', // 表格
'undo', // 撤銷
'redo' // 重復(fù)
]
editor.create()
function getHtml(){
document.getElementById('content').value=editor.txt.html();
}
</script>
后端代碼
<?php
namespace app\upload\controller;
use think\Url;
use think\Controller;
use think\Db;
use think\Request;
use Think\Upload;
class Index extends Controller
{
public function index()
{
// 獲取表單上傳文件 例如上傳了001.jpg
$file = request()->file('image');
// 移動(dòng)到框架應(yīng)用根目錄/public/uploads/ 目錄下
if($file){
$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
if($info){
// 成功上傳后 獲取上傳信息
// 輸出 jpg
$path=$info->getSaveName();
$path=str_replace('\\', '/',"/uploads/".$path);
$file_road=array('errno'=>0,'data'=>array($path));
echo json_encode($file_road);
// echo $info->getExtension();
// // 輸出 20160820/42a79759f284b767dfcb2a0197904287.jpg
// echo $info->getSaveName();
// // 輸出 42a79759f284b767dfcb2a0197904287.jpg
// echo $info->getFilename();
}else{
// 上傳失敗獲取錯(cuò)誤信息
echo $file->getError();
}
}
}
}