ExtJS + fileuploadfield实现文件上传

后台服务端接收文件的代码:

 

/**
  * 后台上传文件处理Action
 */
@RequestMapping(value = "/uploadFile", method=RequestMethod.POST)
public void uploadFile(@RequestParam(value="file",required=true) MultipartFile file ,HttpServletResponse response) {
        ModelMap modelMap = new ModelMap();
        String savePath = "D:/tmp/";//保存路径

try { String fileName = file.getName(); File saveFile = new File(savePath); if(!saveFile.exists()){ saveFile.mkdirs(); } saveFile = new File(savePath, fileName); file.transferTo(saveFile); modelMap.addAttribute("success", true); } catch (Exception e) { modelMap.addAttribute("success", false); modelMap.addAttribute("message", "保存失败:"+e.getMessage()); } JSONSerializer serializer = new JSONSerializer(); String result = serializer.serialize(modelMap); //ExtJS上传需要用这种方法实现返回 response.setContentType("text/html;charset=utf-8"); PrintWriter writer = response.getWriter(); writer.write(result); writer.flush(); writer.close(); }

 

刚开始使用 return modelMap 返回信息,但是前台就是接收不到数据,最后看API后使用PrintWriter来搞定。

 

附上前台上传窗口代码:

UploadForm = Ext.extend(Ext.Window,{
        constructor : function(a){
            Ext.applyIf(this,a);
            this.initUIComponents();
            UploadForm.superclass.constructor.call(this,{
                layout : 'fit',
                modal : true,//遮罩层
                constrain : true,
                width : 500,
                height : 200,
                title : '选择上传文件窗口',
                items : this.formPanel,
                buttonAlign : 'center',
                keys : [{
                    key : Ext.EventObject.ENTER,
                    scope: this,
                    fn: this.uploadFile
                }],
                buttons : [{
                    text : '保存',
                    scope : this,
                    iconCls : "btn-save",
                    handler: this.uploadFile
                },{
                    text : '取消',
                    scope : this,
                    iconCls : "btn-cancel",
                    handler : function(){
                        this.close();
                    }
                }]
            });
        },
        initUIComponents : function(){
            this.formPanel = new Ext.FormPanel({
                layout : 'form',
                fileUpload: true,
                border : false,
                method:'POST', 
                enctype:'multipart/form-data', 
                bodyStyle : 'padding: 10px 10px 0 10px;',
                url : _ctx + '/fuile/uploadFile.do',
                defaults : {
                    anchor : '100%'
                },
                labelAlign : 'right',
                items : [
                            {xtype : 'hidden',name : 'userId',value : this.userId},
                            Ext.Util.buildColumnForm(1,'textfield',{
                                fieldLabel : '备注信息',
                                name : 'remark',
                                allowBlank : false,
                                maxLength : 100,
                                maxLengthText : '信息长度小于等于100个字符'
                            }),
                            {
                                xtype: 'fileuploadfield',
                                id: 'form_file',
                                fieldLabel : '脚本上传',
                                name : 'file',//后台接收
                                emptyText: '请上传word文档', 
                                buttonText: '',
                                regex : /\.(doc|docx)$/,
                                regexText : "请上传word文档",
                                buttonCfg: {
                                    iconCls: 'btn-upload-icon'
                                }
                            }
                ]
            });
        },
        uploadFile : function(){
            var win = this;
            var formFile = Ext.getCmp('form_file').getValue();
            if(this.formPanel.getForm().isValid()){
                if(formFile==''){
                    Ext.Msg.alert("操作提示:", "请上传word文件然后保存");
                    return;
                }
                this.formPanel.getForm().submit({
                    url: ctx + '/file/uploadFile.do',
                    waitMsg: '正在保存...',
                    success : function(form, action){
                        var result = Ext.decode(action.response.responseText);
                        Ext.Msg.alert(result.message, "");
                        win.close();
                    },
                    failure: function(form, action) {
                        var result = Ext.decode(action.response.responseText);
                        Ext.Msg.alert("错误提示", result.message);
                    }
                });
            }
        }
});

调用方法
new UploadForm({userId : '34567'}).show();
 
 

转载于:https://www.cnblogs.com/yzuzhang/p/5128174.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值