ext + struts2 上传文件

来源:互联网 发布:一人一首成名曲网络篇 编辑:程序博客网 时间:2024/05/01 07:25

openAddDocForm = function(node) {
    var fileForm = new Ext.form.FormPanel({
        frame : true,
        url : "addDoc.action",
        bodyStyle : 'padding:5px 5px 0',
        baseCls : 'x-plain',
        defaultType : 'textfield',
        fileUpload: true,   //这里是必须的
        labelAlign : 'left',
        labelWidth : 100,
        items : [{
            fieldLabel : '<font color="blue">文档代号</font>',
            name : "doc.fileCode",
            allowBlank : false,
            blankText : '文档代号不能为空'
        }, {
            fieldLabel : '<font color="blue">选择文件</font>',
            name : 'document',
            inputType : 'file',
            allowBlank : false,
            blankText : '请选择小于100M的文件'
        }, {
            fieldLabel : '备注',
            name : 'doc.comm',
            inputType : 'textarea'
        }, {
            name : 'doc.folder.id',
            value : node.id,
            inputType : 'hidden'
        }],
        buttonAlign : 'center',
        buttons : [{
            text : '保存',
            type : 'submit',
            handler : function() {
                // unitForm.getForm().submit();
                Ext.MessageBox.show({
                    title : '请稍等',
                    msg : '正在保存...',
                    progressText : '',
                    width : 300,
                    progress : true,
                    closable : false,
                    animEl : 'loding'
                });

                var f = function(v) {
                    return function() {
                        var i = v / 11;
                        Ext.MessageBox.updateProgress(i, '');
                    };
                };
                for (var i = 1; i <= 11; i++) {
                    setTimeout(f(i), i * 50);
                }

                fileForm.getForm().doAction('submit', {
                    url : "addDoc.action",
                    method : 'POST',
                    params : '',
                    waitMsg : 'saving...',
                    success : function(form, action) {
                        // Ext.MessageBox.alert('恭喜', action.result);
                        if (action.result.success == true) {
                            fileForm.getForm().reset();
                            refreshFileGrid(node.id);

                            // window.alert(node.reload());
                        }
                        // gridPanel.getStore().reload();
                        // treepanel.getLoader().load();
                    },
                    failure : function(form,action) {
                        if(action.result.success==false){
                            if(action.result.message=='sameName'){
                                    Ext.MessageBox.confirm('警告','存在同名文件,是否覆盖',function(nbt){
                                        if(nbt=='yes'){
                                            fileForm.getForm().doAction('submit', {
                                                    url : "overwriteDoc.action",
                                                    method : 'POST',
                                                    params : '',
                                                    waitMsg : 'saving...',
                                                    success : function(form, action) {
                                                        if (action.result.success == true) {
                                                            fileForm.getForm().reset();
                                                            refreshFileGrid(node.id);
                                                        }else{
                                                           
                                                        }
                                                       
                                                    },
                                                    failure:function(form,action){
                                                        Ext.MessageBox.alert('错误提示','保存失败');
                                                    }
                                            });
                                        }
                                    });
                                }else{
                                    Ext.MessageBox.alert('错误提示', action.result.message);
                                }
                            }
                        }
                    });
                }
            }, {
            text : '关闭',
            handler : function() {
                closeWin();
            }
        }]
    });
    var win = openWin(fileForm);
    win.show();

}

 

 

 

 

 

 

struts.xml

 

<action name="addDoc" class="fileAction" method="addDoc">
          
            <interceptor-ref name="fileUploadStack" />//
            <interceptor-ref name="json" />//拦截器的使用
            <result type="json">

          <param name="savePath">/docUpdate</param>         //如果设置参数上面加上<interceptor-ref name="defaultStack" />

  <param name="includeProperties">success,message</param>
                <param name="contentType">text/html</param> //这里必须有 否则会出现下载页面
            </result>
        </action>

        <action name="overwriteDoc" class="fileAction" method="overwriteDoc">
            <interceptor-ref name="fileUploadStack" />
            <interceptor-ref name="json" />
            <result type="json">
                <param name="includeProperties">success,message</param>
                <param name="contentType">text/html</param>
            </result>
        </action>