- 加载编辑器基本配置
- 配置服务器页面地址
- 配置TP6路由
- 编写上传文件逻辑
- 新建上传文件夹(linux下要开启可写权限)
- 编写页面表单
- 测试
编辑器加载代码(可独立成一个文件随时装填):
var html_content;
var { createEditor, createToolbar } = window.wangEditor;
var editorConfig = {
placeholder: 'Type here...',
onChange(editor) {
html_content = editor.getHtml()
},
MENU_CONF: {}
}
editorConfig.MENU_CONF['uploadImage'] = {
server: 'upload_img',
fieldName: 'custom-field-name',
maxFileSize: 1 * 1024 * 1024,
maxNumberOfFiles: 10,
allowedFileTypes: ['image/*'],
meta: {
token: 'xxx',
otherKey: 'yyy'
},
metaWithUrl: false,
headers: {
Accept: 'text/x-json',
otherKey: 'xxx'
},
withCredentials: true,
timeout: 5 * 1000,
onBeforeUpload(file) {
return 1;
},
onFailed(file, res) {
console.log(`${file.name} 上传失败`);
console.log(res)
},
onSuccess(file, res) {
console.log(`${file.name} 上传成功`, res)
}
}
var editor = createEditor({
selector: '#editor-container',
html: '<p><br></p>',
config: editorConfig,
mode: 'default',
})
var toolbarConfig = {}
var toolbar = createToolbar({
editor,
selector: '#toolbar-container',
config: toolbarConfig,
mode: 'default',
})
tp6后台代码:
public function upload_img()
{
$file_tmp = $_FILES['custom-field-name']['tmp_name'];
$file_name = $_FILES['custom-field-name']['name'];
$date = date("YmdHis");
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
$rand = rand(1000, 9999);
$dir = 'upload2/' . $date . $rand . '.' . $ext;
$dir2 = '/upload2/' . $date . $rand . '.' . $ext;
move_uploaded_file($file_tmp, $dir);
$success =
[
"errno" => 0,
"data" => [
"url" => $dir2,
"alt" => "yyy",
"href" => "zzz"
]
];
$str = json_encode($success);
echo $str;
}
路由配置:
Route::rule('admin/upload_img', 'admin0115/upload_img');