博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
62. ExtJS + fileuploadfield实现文件上传
阅读量:6913 次
发布时间:2019-06-27

本文共 3060 字,大约阅读时间需要 10 分钟。

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

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

 

/**  * 后台上传文件处理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/sharpest/p/7652368.html

你可能感兴趣的文章
《http权威指南》阅读笔记(七)
查看>>
在Eclipse中安装ADT
查看>>
[case48]聊聊flink的SocketClientSink
查看>>
怎样给Swagger换皮肤?
查看>>
网络协议 15 - P2P 协议:小种子大学问
查看>>
# 编写第一个Chrome Extension
查看>>
Python--Redis实战:第五章:使用Redis构建支持程序:第1节:使用Redis来记录日志...
查看>>
2019 简易Web开发指南
查看>>
H5拍照、选择图片上传组件核心
查看>>
单词纠错
查看>>
你可能不知道的14个JavaScript调试技巧
查看>>
nodejs遍历文件夹下并操作HTML/CSS/JS/PNG/JPG
查看>>
根据背景颜色的亮度调整字体的颜色
查看>>
python requests socks代理
查看>>
PHP socket初探 --- 含着泪也要磕完libevent(三)
查看>>
白鹭引擎王泽:重度H5游戏性能优化技巧
查看>>
PHP编译参数configure配置详解(持续更新中)
查看>>
ios 服务器端php推送证书生成
查看>>
多列布局(column)
查看>>
用Python写算法 | 蓄水池算法实现随机抽样
查看>>