日期:2014-05-16 浏览次数:20402 次
Ext.define('org.allenz.UploadFormPanel', { extend : 'Ext.form.Panel', initComponent : function() { var states = Ext.create('Ext.data.Store', { fields : [ 'name', 'value' ], data : [ { name : '成功', value : 0 }, { name : '失败', value : 1 }, { name : '模拟服务器错误', value : 2 } ] }); var me = this; Ext.apply(me, { width : 300, height : 100, items : [ { xtype : 'filefield', fieldLabel : '上传文件', name : 'upload', buttonText : '浏览', allowBlank : false }, { xtype : 'combo', fieldLabel : '上传结果', name : 'state', allowBlank : false, store : states, displayField : 'name', valueField : 'value' } ], dockedItems : [ { xtype : 'toolbar', dock : 'bottom', ui : 'footer', items : [ '->', { text : '上传', scope : me, handler : me.onUpload } ] } ] }); me.callParent(arguments); }, onUpload : function() { var me = this; var form = me.getForm(); if (!form.isValid()) { return; } // 浏览器提示 Ext.Msg.show({ msg : '正在上传...', width : 300, closable : false }); form.submit({ url : 'upload.do', scope : me, success : me.uploadSuccess, failure : me.uploadFailure }); }, uploadSuccess : function(form, action) { Ext.Msg.hide(); Ext.Msg.alert('提示', '上传成功!'); }, uploadFailure : function(form, action) { Ext.Msg.hide(); var errorText; switch (action.failureType) { case Ext.form.action.Action.CLIENT_INVALID: // 浏览器检查失败 errorText = '请选择上传文件!'; break; case Ext.form.action.Action.SERVER_INVALID: // 服务器检查文件失败 errorText = action.result.errors[0].message; break; default: // 网络或服务器错误 errorText = '因网络或服务器错误,上传失败!'; } Ext.Msg.alert('错误', errorText); } });
@Controller public class UploadTestController { @RequestMapping("/upload.do") void upload(HttpServletResponse response, int state) throws IOException { String result = null; switch (state) { case 0: result = "{success:true}"; break; case 1: result = "{success:false,errors:[{message:'文件格式错误!'}]}"; break; case 2: throw new RuntimeException("抛出异常"); } response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); response.resetBuffer(); PrintWriter out = response.getWriter(); out.write(result); out.close(); } }
// 定义错误信息数据模型 Ext.define('org.allenz.ErrorModel', { extend : 'Ext.data.Model', fields : [ 'message' ] }); // 定义上传错误数据读取器 Ext.define('org.allenz.UploadErrorReader', { extend : 'Ext.data.reader.Json', alternateClassName : 'Ext.data.UploadErrorReader', alias : 'reader.upload', // 设置错误信息根节点为errors root : 'errors', // 覆盖getResponseData getResponseData : function(response) { var data; try { // 尝试将respon