<script type="text/javascript" src="/static/admin/libs/tinymce/tinymce.min.js"></script>
<script>
    layui.use(['form', 'layer'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        // 渲染富文本编辑器
        tinymce.init({
            selector: '#content_edit',
            height: 400,
            branding: false,
            language: 'zh_CN',
            plugins: 'code print preview fullscreen paste searchreplace save autosave link autolink image imagetools media table codesample lists advlist hr charmap emoticons anchor directionality pagebreak quickbars nonbreaking visualblocks visualchars wordcount',
            toolbar: 'fullscreen preview code | undo redo | forecolor backcolor | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | outdent indent | numlist bullist | formatselect fontselect fontsizeselect | link image file emoticons charmap anchor pagebreak codesample | ltr rtl',
            toolbar_drawer: 'sliding',
            images_upload_url: '{:url("upload", ["upType" => "editor"])}',
            //images_upload_base_path:'/',
            convert_urls: false,
            file_picker_types: 'file,image,media',
            file_picker_callback: function (callback, value, meta) {
                //文件分类
                var filetype='.pdf, .txt, .zip, .rar, .7z, .doc, .docx, .xls, .xlsx, .ppt, .pptx, .mp3, .mp4';
                //后端接收上传文件的地址
                var upurl='{:url("Ajax/upload", ["upType" => "editor"])}';
                //为不同插件指定文件类型及后端地址
                switch(meta.filetype){
                    case 'image':
                        filetype='.jpg, .jpeg, .png, .gif, .jpeg';
                        upurl='{:url("upload", ["upType" => "editor"])}';
                        break;
                    // case 'media':
                    //     filetype='.mp3, .mp4';
                    //     upurl='upfile.php';
                    //     break;
                    case 'file':
                        filetype='.docx, .doc, .xls, .xlsx, .ppt, .pptx, .pdf, .zip, .rar';
                        upurl='{:url("upload", ["upType" => "editor"])}';
                        break;
                    default:
                }
                //模拟出一个input用于添加本地文件
                var input = document.createElement('input');
                input.setAttribute('type', 'file');
                //input.setAttribute('accept', filetype); 禁用扩展
                input.click();
                input.onchange = function() {
                    var file = this.files[0];
                    var xhr, formData;
                    xhr = new XMLHttpRequest();
                    xhr.withCredentials = false;
                    xhr.open('POST', upurl);
                    xhr.onload = function() {
                        var json;
                        if (xhr.status !== 200) {
                            failure('HTTP Error: ' + xhr.status);
                            return;
                        }
                        json = JSON.parse(xhr.responseText);
                        if (!json || typeof json.location !== 'string') {
                            failure('Invalid JSON: ' + xhr.responseText);
                            return;
                        }
                        callback(json.location, { title: file.name });
                    };
                    formData = new FormData();
                    formData.append('file', file, file.name );
                    xhr.send(formData);
                };
            },
            init_instance_callback: function (editor) {
                // 编辑器初始化完成以后的
                layer.closeAll('loading');
            }
        });

</script>

callback(json.location, { title: file.name });这里容易出错,原文是 callback(json.location);js报错。

标签: TinyMCE

添加新评论