zk 上传附件

<?page cacheable="false"     language="xul/html" zscriptLanguage="Java" contentType="text/html;charset=UTF-8"?>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<zk xmlns="http://www.zkoss.org/2005/zul"
    xmlns:h="http://www.w3.org/1999/xhtml"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:y="http://www.zkoss.org/2007/yui"
    xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul/zul.xsd">
    <window id="uploadFileWin" width="480px" visible="true" title="上传附件"
        closable="true" border="normal" apply="${ealAccountUploadFileController}">
        <separator spacing="10"></separator>
        <grid>
            <columns>
                <column width="15%" />
                <column width="85%" />
            </columns>
            <rows>
                <row>
                    <label value="附件:" />
                    <hbox>
                        <label width="250px" id="fileName" value="" style="color:red" />
                        <button label="上传附件"
                            onClick="controller.uploadFile()">
                        </button>
                    </hbox>
                </row>
            </rows>
        </grid>
        <div align="center">
            <button label="确定" width="80px"
                onClick="controller.create()" />
            <button label="取消" width="80px"
                onClick="uploadFileWin.detach()" />
        </div>
    </window>
</zk>


package com.ceair.ealfms.account.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.zkoss.util.media.Media;
import org.zkoss.zk.ui.event.UploadEvent;
import org.zkoss.zul.Fileupload;
import org.zkoss.zul.api.Label;

import cn.sh.cares.web.zk.controller.BaseController;

import com.cares.cjframe.upload.entity.UploadFileVO;
import com.ceair.ealfms.account.dto.AccountUploadFileEditDto;
import com.ceair.ealfms.account.service.EalAccountUploadFileService;
import com.ceair.ealfms.account.util.Constants;


@Component
@Scope("prototype")
public class EalAccountUploadFileController extends BaseController {

    private static final long serialVersionUID = -5460578511811020938L;
    @Autowired
    private EalAccountUploadFileService accountuploadFileService;
    
    

    private List<Media> mediaList;

//    public static List list = new ArrayList();
    
    private Long seq;
    private Long accDetailId;

    public void doAfterCompose(org.zkoss.zk.ui.Component comp) throws Exception {
        super.doAfterCompose(comp);
        seq = getArgValue(Long.class, "seq");
        accDetailId=getArgValue(Long.class, "accountDetailId");
    }
    
    public void uploadFile(UploadEvent uploadEvent) throws InterruptedException {
        Media[] media = uploadEvent.getMedias();
        if (media == null)
            return;
        String val = ((Label) self.getFellow("fileName")).getValue();
        if (val == null)
            val = "";
        if (null == mediaList) {
            mediaList = new ArrayList<Media>();
        }
        boolean flag = false;
        for (int i = 0; i < media.length; i++) {
            String lastPeriod = media[i].getName().substring(media[i].getName().lastIndexOf("."));
            if(lastPeriod != null && "exe,bat".indexOf(lastPeriod) != -1){
                showWarningBox("禁止上传.exe,.bat可执行文件");
                return;
            }
            if(media[i].getByteData().length > 1024*1024*10){
                showWarningBox("上传文件大小超过10M!");
                return;
            }
            for (int j = 0; j < mediaList.size(); j++) {                
                if (media[i].getName().equals(mediaList.get(j).getName())) {
                    flag = true;
                    break;
                }
            }
            if (flag == false) {
                mediaList.add(media[i]);
                val += " " + media[i].getName();
                if (i < media.length - 1)
                    val += ",";
            }
        }
        media = null;
        ((Label) self.getFellow("fileName")).setValue(val);
    }

    /**
     * 上传附件
     *
     * @throws InterruptedException
     */
    public void uploadFile() throws InterruptedException {
        Media[] media = Fileupload.get(1, true);
        if (media == null)
            return;
        String val = ((Label) self.getFellow("fileName")).getValue();
        if (val == null)
            val = "";
        if (null == mediaList) {
            mediaList = new ArrayList<Media>();
        }
        boolean flag = false;
        for (int i = 0; i < media.length; i++) {
            String lastPeriod = media[i].getName().substring(media[i].getName().lastIndexOf(".")+1);
            if(lastPeriod != null && "exe,bat".indexOf(lastPeriod) != -1){
                showWarningBox("禁止上传.exe,.bat可执行文件");
                return;
            }
            if(media[i].getByteData().length > 1024*1024*10){
                showWarningBox("上传文件大小超过10M!");
                return;
            }
            for (int j = 0; j < mediaList.size(); j++) {
                if (media[i].getName().equals(mediaList.get(j).getName())) {
                    flag = true;
                    break;
                }
            }
            if (flag == false) {
                mediaList.add(media[i]);
                val += " " + media[i].getName();
                if (i < media.length - 1)
                    val += ",";
            }
        }
        media = null;
        ((Label) self.getFellow("fileName")).setValue(val);
    }

    /**
     * 新增
     *
     * @throws Exception
     */    
    public void create() throws Exception {
        String ss = this.getParentControllerFromVar().toString();
        String[] sa = ss.split("\\.");
        String s1 = sa[sa.length - 1];
        String[] sa1 = s1.split("@");
        String pupName  = sa1[0];
                
        AccountUploadFileEditDto uploadFileEditDto = new AccountUploadFileEditDto();
        uploadFileEditDto.setMediaList(mediaList);
        
        if ("EalAccountConfirmController".equals(pupName)) {
            uploadFileEditDto.setSourceType(Constants.UPLOAD_FILE_TYPE_ACCOUNT_INPUT);
        }else{
            uploadFileEditDto.setSourceType(Constants.UPLOAD_FILE_TYPE_ACCOUNT);
        }
                
        List<UploadFileVO> fileList = accountuploadFileService
                .doUploadFile(uploadFileEditDto,seq,accDetailId);
        if (fileList == null) {
            showWarningBox("请选择上传附件");
            return;
        }        
        
        self.detach();
        
        if (accDetailId != null) {
            seq = accDetailId;
        }
        
        if ("EalAccountApplyEditController".equals(pupName)) {
            ((EalAccountApplyEditController) this.getParentControllerFromVar())
                    .refreshUploadFileList(seq);
        } else if ("EalAccountEditController".equals(pupName)) {
            ((EalAccountEditController) this.getParentControllerFromVar())
                    .refreshUploadFileList(seq);
        } else if ("EalAccountShowController".equals(pupName)) {
            ((EalAccountShowController) this.getParentControllerFromVar())
                    .refreshUploadFileList();
        } else if ("EalAccountConfirmController".equals(pupName)) {
            ((EalAccountConfirmController) this.getParentControllerFromVar())
                    .refreshUploadFileList(seq);
        } else if ("EalAccountApplyShowController".equals(pupName)) {
            ((EalAccountApplyShowController) this.getParentControllerFromVar())
                    .refreshUploadFileList();
        } else if ("EalAccountApplyAuditController".equals(pupName)) {
            ((EalAccountApplyAuditController) this.getParentControllerFromVar())
                    .refreshUploadFileList();
        }
    }
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值