# 富文本上传图片
html代码
~~~
<div class="layui-form-item layui-form-text">
<div class="layui-input-block">
<textarea id="L_content" name="content" placeholder="请输入内容" class="layui-textarea fly-editor" style="height: 260px;">{$info.content}</textarea>
</div>
<label for="L_content" class="layui-form-label" style="top: -2px;">
内容
</label>
</div>
~~~
js代码
~~~
layui.use(['form', 'layer', 'layedit'], function() {
$ = layui.jquery;
var form = layui.form,
layer = layui.layer,
layedit = layui.layedit;
layedit.set({
uploadImage: {
url: "{:url('upload')}", //接口url
type: 'post', //默认post
success:function(res){
}
}
})
//创建一个编辑器
editIndex = layedit.build('L_content');
});
~~~
php
~~~
public function upload(){
$file = request()->file('file');
$rootPath = Env::get('root_path');
if($file){
$info = $file->rule(function(){
$fileName = date('YmdHis');
return $fileName;
})->move($rootPath . 'public' . DIRECTORY_SEPARATOR . 'uploads/news/');
if($info){
$saveName = $info->getSaveName();
$data = [];
$data['src'] = '/uploads/news/'.$saveName;
return json(['code' => 0,'msg' => '上传成功', 'data'=>$data]);
}else{
// 上传失败获取错误信息
$errorInfo = $file->getError();
return json(['code' => 1,'msg' => $errorInfo]);
}
}
return json(['code' => 1,'msg' => '上传失败']);
}
~~~