今天在项目中遇到一个棘手的问题,在上传附件的时候,window弹窗,第一次的时候是正常的,win.hide() 后,第二次fieldLabel值会重复显示,如下图:
因为window窗口中只有“附件”重复了,“备注”字段没有重复,所以怀疑是因为“附件”的写法有问题
“附件”字段对应的代码如下,用的 inpuType:'file'
name : "uploadFile",
// id : "uploadFile",
inputType : 'file',
fieldLabel : "<font color='red'>*</font>附件",
style :"margin:4px",
height : 20,
buttonText: '浏览',
allowBlank : false,
blankText : "附件不能为空",
emptyText : "请选择附件..."
现在改变文件类型的写法,
inputType : 'file' 改成 xtype:'fileuploadfield'
由于程序中用的dwr,这样改后无法上传文件,具体原因可以参考另一篇博客,http://blog.csdn.net/afei3418/article/details/46972607 有具体的修改方式
这样再次打开window后显示正常:
详细代码如下:
<span style="white-space:pre"> </span>
js代码
//文件panel
fileFormPanel = new Ext.form.FormPanel({
name : "fileFormPanel",
id : "fileFormPanel",
labelWidth : 70,
defaultType : "textfield",
baseCls : 'x-plain',
bodyStyle : 'padding:5px 5px 0',
width : 390,
buttonAlign:'center',
fileUpload: true
loadMask : new Ext.LoadMask(Ext.getBody(), {
msg : '请稍候,正在查询....'
}),
waitMsg : '请稍候,正在查询....',
onSubmit: Ext.emptyFn,
border : false,
defaults : {
width : 230
},
items : [
{
name : "file",
id : "uploadFile",
// inputType : 'file',
xtype:'fileuploadfield',
fieldLabel : "<font color='red'>*</font>附件",
style :"margin:4px",
height : 20,
buttonText: '浏览',
allowBlank : false,
blankText : "附件不能为空",
emptyText : "请选择附件..."
}
,{
name : 'fileRemark',
id : 'fileRemark',
xtype : 'textarea',
fieldLabel: '备注',
collapsible : true,
style : 'margin:4px;margin-bottom:10px;',
width : 230,
height: 100
}],
buttons : [{
name : 'fileSureBtn',
id : 'fileSureBtn',
text : "确定",
handler : function(){
if (!fileWin.getComponent('fileFormPanel').form.isValid()) {
selInfo("请完整填写表单!");
return;
}
//取消遮罩
//fileFormPanel.getEl().mask('请稍后,正在上传附件……');
//进度框
Ext.Msg.wait('请稍后,正在上传附件……', '', {animate : true});
var uploadFile = fileFormPanel.getForm().findField("uploadFile").getValue();
var remark = fileFormPanel.getForm().findField("fileRemark").getValue();
//当前节点对象
var row =grid.getSelectionModel().getSelected();//获取一行
var fId = row.get('FolderId');
var dId = row.get('DocId');
var file = dwr.util.getValue("file");
//构造参数对象
var parObj = {
docId : dId,
folderId : fId,
fileCode : '', //附件编码先不做(预留字段)
fileName : fileName, //文件的名称
uploadFile : uploadFile, //上传的文件全路径
userId : userId,
remark : remark
};
//调用DWR方法
FolderHandlerBean.createFile(file,parObj,function(data){
if(data.flag == '1'){
alert("111");
//隐藏窗口
Ext.getCmp("uploadFile").reset();
fileWin.hide();
//刷新grid
// selOk(data.msg);
Ext.MessageBox.show({
title : '提示',
msg : '添加成功!',
modal : true,
buttons : Ext.Msg.OK,
icon : Ext.Msg.INFO,
width : 200,
fn : function() {
fileGrid.getStore().reload();
}
});
}else{
Ext.getCmp('uploadFile').reset();
fileWin.hide();
selError(data.msg);
}
});
}
}, {
text : "取消",
handler : function() {
//清除数据
Ext.getCmp('uploadFile').reset();
Ext.getCmp('fileRemark').setValue('');
//隐藏窗口
fileWin.hide();
}
}]
});
//文件窗口
fileWin = new Ext.Window({
id : "fileWin",
title : "附件编辑",
width : 460,
height : 450,
bodyStyle : 'padding:5px;',
maximizable : false,
closeAction : 'hide',
closable : true, //是否关闭
collapsible : true, //是否可以收缩
draggable : true, //是否可以拖拽
resizable : false, //是否可以调整大小
modal : true, //是否遮罩(true的时候,后面的页面不能使用)
buttonAlign : "center",
items : fileFormPanel
});
java实现类
/**
* DWR方式进行文件上传
* **/
public Map createFile(FileTransfer ft,Map pMap){
log.info("------------------>调用DWR方法createFile()...");
Map retMap = new HashMap();
Connection con = null;
Statement st = null;
ResultSet rs = null;
try {
String docId = ToolUtil.null2Empty(pMap.get("docId"));
String folderId = ToolUtil.null2Empty(pMap.get("folderId"));
String fileCode = ToolUtil.null2Empty(pMap.get("fileCode"));
//String fileName = ToolUtil.null2Empty(pMap.get("fileName"));
String uploadFile= ToolUtil.null2Empty(pMap.get("uploadFile"));
int haveEndInt = uploadFile.lastIndexOf("\\");
int pointInt = uploadFile.lastIndexOf(".");
String fileName = uploadFile.substring(haveEndInt+1,pointInt);
String userId = ToolUtil.null2Empty(pMap.get("userId"));
String remark = ToolUtil.null2Empty(pMap.get("remark"));
log.info("---------------->uploadFile="+uploadFile);
//dwr获得session值
WebContext ctx = WebContextFactory.get();
HttpServletRequest request = ctx.getHttpServletRequest();
HttpSession session = request.getSession();
String sessionUserId = (String)session.getAttribute("sessionUserId");
ServletContext application = session.getServletContext();
String applicationUserId = (String) application.getAttribute("applicationUserId");
DBPoolUtil db = new DBPoolUtil();
con = db.getConnection();
st = con.createStatement();
//根据目录,查询路径
rs = st.executeQuery("SELECT '/'||REPLACE(F.FULL_CODE,'-','/'),PDS_PUB_SEQ.NEXTVAL FROM PDS_FOLDER F WHERE F.FOLDER_ID = '"+folderId+"'");
String path = "";
long fileId = 0;
if(rs.next()){
path = rs.getString(1);
fileId = rs.getLong(2);
}
//获取文件的后缀
String ext=uploadFile.substring(uploadFile.lastIndexOf(".")).toLowerCase();
//创建新文件的名称,以ID的形式命名
String newFileName = fileId+ext;
//构造工作流
InputStream is = ft.getInputStream();
//FTP上传文件
log.info("上传path-------------》" + path +"\n newFileName:=" + newFileName);
retMap = fhm.uploadFile(path, newFileName, is);
if("0".equals(retMap.get("flag"))){
return retMap;
}
String insertPath = (String) retMap.get("filePath");
//调用存储过程,删掉该目录下的所有的子目录及其附件
CallableStatement cs = con.prepareCall("{call PDS_BASE_PKG.P_ADD_FILE(?,?,?,?,?,?,?,?,?,?)}");
//参数赋值
cs.setLong(1, fileId);
cs.setLong(2, Long.valueOf(docId));
cs.setLong(3, Long.valueOf(folderId));
cs.setString(4, fileCode);
cs.setString(5, fileName);
cs.setLong(6, Long.valueOf(userId));
cs.setString(7, remark);
cs.setString(8, newFileName);
cs.setString(10, insertPath);
//返回类型赋值类型
cs.registerOutParameter(9, Types.VARCHAR);
//执行存储过程
cs.execute();
//获取返回参数
String ret = cs.getString(9);
log.info("----------->调用存储过程后,返回值:"+ret);
if("0".equals(ret)){
con.rollback();
retMap.put("flag", "0");
retMap.put("msg", "后台异常,调用存储过程失败,请查看返回参数类型是否正确!");
return retMap;
}
//提交事务
con.commit();
cs.close();
con.close();
} catch (Exception e) {
log.error(e.getMessage(),e);
retMap.put("flag", "0");
retMap.put("msg", "系统错误,请与管理员联系!");
return retMap;
}finally{
try {
if(rs!=null){rs.close();}
if(st!=null){st.close();}
if(con!=null){con.close();}
} catch (SQLException e) {
log.error(e.getMessage(),e);
}
}
retMap.put("flag", "1");
retMap.put("msg", "保存成功!");
return retMap;
}