`
宫庆义
  • 浏览: 16670 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

HTMLEditor 上传图片 组件

阅读更多


用Extjs做项目,用到htmleditor控件,唯一的缺陷是不可以上传图片,为了以后方便,在基于htmleditor控件上写了一个支持上传图片的。

控件StarHtmleditor

/**
* 重载EXTJS-HTML编辑器
*
* @class HTMLEditor
* @extends Ext.form.HtmlEditor
* @author wuliangbo
*/
HTMLEditor = Ext.extend(Ext.form.HtmlEditor, {
    addImage : function() {
        var editor = this;
        var imgform = new Ext.FormPanel({
            region : 'center',
            labelWidth : 55,
            frame : true,
            bodyStyle : 'padding:5px 5px 0',
            autoScroll : true,
            border : false,
            fileUpload : true,
            items : [{
                        xtype : 'textfield',
                        fieldLabel : '选择文件',
                        name : 'userfile',
                        inputType : 'file',
                        allowBlank : false,
                        blankText : '文件不能为空',
                        height : 25,
                        anchor : '90%'
                    }],
            buttons : [{
                text : '上传',
                type : 'submit',
                handler : function() {
                    if (!imgform.form.isValid()) {return;}
                    imgform.form.submit({
                        waitMsg : '正在上传',
                        url : 'Default.aspx',
                        success : function(form, action) {
                            var element = document.createElement("img");
                            element.src = action.result.fileURL;
                            if (Ext.isIE) {
                                editor.insertAtCursor(element.outerHTML);
                            } else {
                                var selection = editor.win.getSelection();
                                if (!selection.isCollapsed) {
                                    selection.deleteFromDocument();
                                }
                                selection.getRangeAt(0).insertNode(element);
                            }
                            win.hide();
                        },
                        failure : function(form, action) {
                            form.reset();
                            if (action.failureType == Ext.form.Action.SERVER_INVALID)
                                Ext.MessageBox.alert('警告',
                                        action.result.errors.msg);
                        }
                    });
                }
            }, {
                text : '关闭',
                type : 'submit',
                handler : function() {
                    win.close(this);
                }
            }]
        })

        var win = new Ext.Window({
                    title : "上传图片",
                    width : 300,
                    height : 200,
                    modal : true,
                    border : false,
                    iconCls : "picture.png",
                    layout : "fit",
                    items : imgform

                });
        win.show();
    },
    createToolbar : function(editor) {
        HTMLEditor.superclass.createToolbar.call(this, editor);
        this.tb.insertButton(16, {
                    cls : "x-btn-icon",
                    icon : "picture.png",
                    handler : this.addImage,
                    scope : this
                });
    }
});
Ext.reg('StarHtmleditor', HTMLEditor);


页面js代码

Ext.onReady(function() {
    Ext.QuickTips.init();
    Ext.form.Field.prototype.msgTarget = 'side';
    var ff = new Ext.FormPanel({
                title : "文件上传",
                renderTo : document.body,
                width : 600,
                height : 480,
                labelWidth : 55,
                frame : true,
                items : [{
                            xtype : "textfield",
                            name : "title",
                            fieldLabel : "标题",
                            anchor : "98%"
                        }, {
                            xtype : "combo",
                            name : "topic_id",
                            fieldLabel : "所属栏目",
                            anchor : "98%"
                        }, {
                            xtype : "textfield",
                            name : "keywords",
                            fieldLabel : "关键字",
                            anchor : "98%"
                        }, {
                            xtype : "StarHtmleditor",
                            name : "content",
                            fieldLabel : "内容",
                            anchor : "98%"
                        }]
    });

});

后台代码简单实现了一下

protected void Page_Load(object sender, EventArgs e)
        {
            string fileName = string.Empty;
            string fileURL = string.Empty;
            string rt = string.Empty;
            try
            {
                HttpPostedFile file = Request.Files[0];
                fileName = GetFileName(file.FileName);
                file.SaveAs(Server.MapPath("upload\\") + fileName);
                fileURL = "upload/" + fileName;
                rt = "{success:'true',fileURL:'" + fileURL + "'}";
            }
            catch
            {
                rt = "{success:'false',fileURL:'" + fileURL + "'}";
            }
            
            Response.Write(rt);

        }
       
        private string GetFileName(string FullName)
        {
            string fileName = string.Empty;
            int last = FullName.LastIndexOf(@"\");
            fileName = FullName.Substring(last + 1, FullName.Length - last - 1);
            return fileName;
        }


实现效果如下




本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhaozhen1984/archive/2010/09/28/5911839.aspx
分享到:
评论

相关推荐

    ExtJS的编辑器HtmlEditor的一个图片插件

    HtmlEditor(编辑器)的一个图片插件,制作过程用到了图片上传等,采用Struts1.2 ,上传组件使用的是Cos,这个插件的意义并不在于插件本身,只是希望给要用ExtJS做文件上传或想做ExtJS插件的朋友提供一种思路 ...

    HTML在线编辑器(网峰科技专用)

    4. 高效的ASP无组件上传,用过你就知道 5. 其他特色请您自己去发掘 6. 此编辑相关功能:文件包及上传部分为ASP代码, 如果你有兴趣修改成其他代码或你有好的修改或建议,请回传给我,谢谢! ...

    JAVA后台管理系统源码

    3、ExtJS的HtmlEditor的图片文件上传插件。 4、Grid列表和表单,包含添加、删除、批量删除、修改、查看、图片查看和按条件查询列表等功能。 5、导入导出Excel数据,支持xlsx和xls文件。 6、资源管理(菜单管理)。 7...

    通用后台管理系统(ExtJS 4.2 + WebMatrix).zip

    2、ExtJS的HtmlEditor的图片文件上传插件。 3、Grid列表和表单,包含添加、删除、批量删除、修改、查看、图片查看和按条件查询列表等功能。 4、导入导出Excel数据,支持xlsx和xls文件。 5、资源管理(菜单管理)。 6...

    ExtJS 4.2+JAVA通用后台管理系统(ExtJS 4.2+Hibernate 4.1.7+Spring MVC 3.2.8)

    3、ExtJS的HtmlEditor的图片文件上传插件。 4、Grid列表和表单,包含添加、删除、批量删除、修改、查看、图片查看和按条件查询列表等功能。 5、导入导出Excel数据,支持xlsx和xls文件。 6、资源管理(菜单管理)。 7...

    Extjs完整大型项目.rar

    3、ExtJS的HtmlEditor的图片文件上传插件。 4、Grid列表和表单,包含添加、删除、批量删除、修改、查看、图片查看和按条件查询列表等功能。 5、导入导出Excel数据,支持xlsx和xls文件。 6、资源管理(菜单...

    Java通用后台管理系统源码 JAVATYHTXT.rar

    3、ExtJS的HtmlEditor的图片文件上传插件。 4、Grid列表和表单,包含添加、删除、批量删除、修改、查看、图片查看和按条件查询列表等功能。 5、导入导出Excel数据,支持xlsx和xls文件。 6、资源管理(菜单管理)。 7...

    最新JAVA通用后台管理系统(ExtJS 4.2+Hibernate 4.1.7+Spring MVC 3.2.8)MyEclipse版本

    3、ExtJS的HtmlEditor的图片文件上传插件。 4、Grid列表和表单,包含添加、删除、批量删除、修改、查看、图片查看和按条件查询列表等功能。 5、导入导出Excel数据,支持xlsx和xls文件。 6、资源管理(菜单管理)。 7...

    最新JAVA通用后台管理系统(ExtJS 4.2+Hibernate 4.1.7+Spring MVC 3.2.8)Eclipse版本

    3、ExtJS的HtmlEditor的图片文件上传插件。 4、Grid列表和表单,包含添加、删除、批量删除、修改、查看、图片查看和按条件查询列表等功能。 5、导入导出Excel数据,支持xlsx和xls文件。 6、资源管理(菜单管理)。 7...

    在线编辑器 Web Wiz Rich Text Editor v4.03 最新版

    内容索引:ASP源码,其它类别,在线编辑器,网页编辑器,HTMLeditor...支持图片上传,在多种浏览器下都能正常使用,无ActiveX组件,使用wordtidy :trade_mark:技术使粘贴的内容更清洁。插入图片,文件,表格等功能也很强大。

    Ext Js权威指南(.zip.001

    8.4.1 组件管理及查询:ext.componentmanager与ext.componentquery / 423 8.4.2 焦点管理:ext.focusmanager / 424 8.4.3 z-order管理:ext.zindexmanager与ext.windowmanager / 425 8.4.4 状态管理:ext.state....

    ExtAspNet_v2.3.2_dll

    -修正HtmlEditor不能编辑的BUG(feedback:TheBox)。 -修正IE下有时会出现空白页面的情况(feedback:olivia919)。 +2009-12-06 v2.1.8 -修正了使用IFrame的Window关闭后不能再次打开的BUG(feedback:alexa99)。 ...

    ExtAspNet v2.2.1 (2009-4-1) 值得一看

    -修正HtmlEditor不能编辑的BUG(feedback:TheBox)。 -修正IE下有时会出现空白页面的情况(feedback:olivia919)。 +2009-12-06 v2.1.8 -修正了使用IFrame的Window关闭后不能再次打开的BUG(feedback:alexa99)。 ...

Global site tag (gtag.js) - Google Analytics