表单中,增加单个上传附件功能

 

 1、修改的相关文件

相关文件

2、AttachmentManager.java新增方法

/**
     * 根据punid, 返回一个附件
     * 
     * @param punid
     * @return
     */
    public static Attachment getOneFirst(String punid, String flag) {
    	
        String hql = " from Attachment where punid = '" + punid + "' and flag='" + flag + "'"; 
        hql += " order by createTime desc ";
        Attachment attr = (Attachment)HibernateWrapper.queryOneFirstObject(hql);
        
        return attr;
    }

3、CarInfoAction.java 添加修改,新增和删除的附件方法,并引用

  • 在Action中添加方法
/**
     * 获取更新操作的时候,得到修改,新增和删除的附件列表
     * 
     * @param session
     * @param updateObjs 更新的列表对象
     * @param addObjs 新增的列表对象
     * @param stuffDelHqls 删除的列表对象
     * @param delUnids 要删除的附件的UNID
     */
    private void addUpdateAttr(Session session,List<String> stuffDelHqls, List<Object> addObjs, List<Object> updateObjs, String punid ) {
    	
    	CfgUser user = (CfgUser) session.getRequest().getSession().getAttribute(LoginAction.ATTRIBUTE_USER);
        
    	// 删除指定附件
    	if(stuffDelHqls != null && !"".equals(StrUtil.checkNull(punid))){
    		stuffDelHqls.add("delete Attachment where punid = '" + punid + "'");
    	}
        
    	//添加附件
        List files = FileUploadManger.getUpFile(session);
        if(null != files && files.size() > 0 ){
	        for (int i = 0; i < files.size(); i++) {
	            FileUploadObj fileObj = (FileUploadObj) files.get(i);
	            Attachment tblstuffinfo = new Attachment();
	            tblstuffinfo.setUnid(UNIDGenerate.getKey());
	            tblstuffinfo.setPunid(punid);
	
	            tblstuffinfo.setName(fileObj.getFileName());
	            tblstuffinfo.setExplain("");
	            tblstuffinfo.setNodename("");
	            tblstuffinfo.setNodeunid("");
	            tblstuffinfo.setContent(fileObj.getFileEntity());
	            tblstuffinfo.setType(fileObj.getFileType());
	            tblstuffinfo.setRemark("");// 设置默认值
	            tblstuffinfo.setFlag("1");
	            tblstuffinfo.setOperator(user.getUnid());
	            tblstuffinfo.setOperatorname(user.getRealname());
	            tblstuffinfo.setCreateTime(DateUtil.getNowDateTime());
	            addObjs.add(tblstuffinfo);
	        }
        }
        
    }
  • 在action的新增区域调用
// 增加附件
                addUpdateAttr(session, null, addObjs, null, unid);
  • 在action的代码更新区域调用
// 重新上传附件并删除旧附件
                addUpdateAttr(session, hqls, addObjs, null, unid);

4、car_info_edit.jsp

  • 表单form元素增加属性 enctype="multipart/form-data"
  • 增加元素
<td class="right">保养须知:</td>
			      	<td class="inputborder">
			      	<%
			      	String dis = "none";
			      	String stuffunid = "";
			      	String stuffname = "";
					if("edit_update".equals(formaction)){
						Attachment stuff = AttachmentManager.getOneFirst(unid,"1");
						if(null != stuff){
							dis = "";
							stuffunid = stuff.getUnid();
							stuffname = stuff.getName();
						}
					}
			      	%>
			      		<a id="a_xz" href="javascript:downloadsave('<%=stuffunid %>');" style="display:<%=dis%>" title="<%=StrUtil.checkNull(stuffname)%>">
			      			<%=StrUtil.getTextLimitLength(StrUtil.checkNull(stuffname),20) %>
			      		</a>
						<input type="button" name="a_cxsc" id="a_cxsc" value="重新上传" onclick="showupload()" class="btn_2k3" style="display:<%=dis%>">
			      		<input type="file" name="input_file" id="input_file" style="display:<%="".equals(dis) ? "none" : ""%>;width:200px"/>
			      		<input type="button" name="input_fh" id="input_fh" value="返回" class="btn_2k3" style="display:none" onclick="fh()"/>
			      		<input type="hidden" name="stuffname" id="stuffname" value="<%=stuffname %>"/>
			      		<input type="hidden" name="stuff_cz" id="stuff_cz" value=""/><!-- 附件操作标识 删除附件0、增加附件1、重新上传2 -->
			      	</td>
  •  脚本控制上传输入框的显示与隐藏
//显示文件上传控件
	function showupload() {
		document.getElementById("a_xz").style.display = "none";
		document.getElementById("a_cxsc").style.display = "none";
		document.getElementById("input_file").style.display = "inline";
		document.getElementById("input_fh").style.display = "inline";
		document.getElementById("stuff_cz").value = "1";
	}
	
	//返回
	function fh(){
		document.getElementById("a_xz").style.display = "inline";
		document.getElementById("a_cxsc").style.display = "inline";
		document.getElementById("input_file").style.display = "none";
		document.getElementById("input_fh").style.display = "none";
		document.getElementById("stuff_cz").value = "";
	}
	
	//下载附件到本地
	function downloadsave(unid){
		 window.open('<%=request.getContextPath()%>/downloadFile?readType=db&unid=' + unid);
	}
 

猜你喜欢

转载自ad8224.iteye.com/blog/1493927