ueditor设置域之后单图上传解决方案

来源:互联网 发布:手机送货单软件 编辑:程序博客网 时间:2024/06/10 09:53

注:需要用到jquery包,ueditor插件包

//页面创建上传按钮进行隐藏<input id="file_editor_upimg" type="file" accept="image/gif,image/jpeg,image/png,image/jpg,image/bmp" />
//以下代码封装成JS包引入即可UE.registerUI('button', function (editor, uiName) {    //注册按钮执行时的command命令,使用命令默认就会带有回退操作    editor.registerCommand(uiName, {        execCommand: function () {            alert('execCommand:' + uiName);        }    });    //创建一个button    var btn = new UE.ui.Button({        //按钮的名字        name: uiName,        //提示        title: "图片上传",        //需要添加的额外样式,指定icon图标,这里默认使用一个重复的icon        cssRules: 'background-position: -380px 0px;',        //点击时执行的命令        onclick: function () {            //这里可以不用执行命令,做你自己的操作也可            //editor.execCommand(uiName);            $('#file_editor_upimg').val('');            $('#file_editor_upimg').click();            $('#file_editor_upimg').on('change', function (e) {                var formData = new FormData();                formData.append('imgFileData', $('#file_editor_upimg')[0].files[0]);                $.ajax({                    url: '/Headlines/OssUpload',                    type: 'POST',                    dataType: "json",                    cache: false,                    data: formData,                    processData: false,                    contentType: false,                    success: function (ret) {                        //返回枚举类型                        if (ret.state == 1) {                            var imgHtml = '<p><img src="' + ret.url + '" title= "" alt= "' + ret.url + '" /></p>';                            UE.getEditor('editor').execCommand('insertHtml', imgHtml);                            $(this).unbind(e);                        } else {                            $.messager.alert('提示', ret.msg, 'warning');                        }                    }                });            });        }    });    //当点到编辑内容上时,按钮要做的状态反射    editor.addListener('selectionchange', function () {        var state = editor.queryCommandState(uiName);        if (state == -1) {            btn.setDisabled(true);            btn.setChecked(false);        } else {            btn.setDisabled(false);            btn.setChecked(state);        }    });    //因为你是添加button,所以需要返回这个button    return btn;}/*index 指定添加到工具栏上的那个位置,默认时追加到最后,editorId 指定这个UI是那个编辑器实例上的,默认是页面上所有的编辑器都会添加这个按钮*/);