gwt 前端上传组件

阅读更多

package com.appdev.bsf.client.file;

import com.appdev.bsf.common.client.messagebox.MessageBox;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitHandler;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;


public class GwtFileUploadLayout extends HLayout {
    private FormPanel formPanel = null;
    /**
     *
     * @param submitHandler
     *            不可以为null,

     *            设置action,例如:formPanel.setAction(GWT.getModuleBaseURL() +"fileupload?id=1&clsUrl=" + Object.class);

     *            //id:为当前记录的id值,clsUrl:当前操作实体类全路径(注意参数名字必须一样)

     *            if (fileUpLoad.getFilename().length() == 0) {

     *                MessageBox.getInstance().initComponents("必须选择一个文件");

     *                event.cancel();

     *            } else if (!fileUpLoad.getFilename().toLowerCase().endsWith(".jpg")

     *                    && !fileUpLoad.getFilename().toLowerCase().endsWith(".gif")) {

     *                event.cancel();

     *            }//验证

     * @param submitCompleteHandler
     *            可以为null,用于上传至服务之后可以做其它操作
     */
    public GwtFileUploadLayout(SubmitHandler submitHandler, SubmitCompleteHandler submitCompleteHandler) {
        setWidth(200);
        final FileUpload fileUpLoad = new FileUpload();
        fileUpLoad.setName("imageFileUpload");
        formPanel = new FormPanel();
        formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
        formPanel.setMethod(FormPanel.METHOD_POST);
        formPanel.setWidget(fileUpLoad);
        final IButton btnUpload = new IButton("upLoadButton");
        btnUpload.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                formPanel.submit();
            }
        });

        formPanel.addSubmitHandler(new SubmitHandler() {

            @Override
            public void onSubmit(SubmitEvent event) {
                formPanel.setAction(GWT.getModuleBaseURL() + "fileupload?id=1&clsUrl=" + Object.class);
                if (fileUpLoad.getFilename().length() == 0) {
                    MessageBox.getInstance().initComponents("必须选择一个文件");
                    event.cancel();
                } else if (!fileUpLoad.getFilename().toLowerCase().endsWith(".jpg")
                        && !fileUpLoad.getFilename().toLowerCase().endsWith(".gif")) {
                    event.cancel();
                }

            }
        });
        if (submitCompleteHandler == null) {
            formPanel.addSubmitCompleteHandler(new SubmitCompleteHandler() {

                @Override
                public void onSubmitComplete(SubmitCompleteEvent event) {
                    String result = event.getResults();
                    if ("".equals(result)) {
                        MessageBox.getInstance().initComponents("文件上传成功");
                    } else {
                        MessageBox.getInstance().initComponents("文件上传失败");
                    }
                }
            });
        } else {
            formPanel.addSubmitCompleteHandler(submitCompleteHandler);
        }
       
        addMember(formPanel);
        addMember(btnUpload);
    }
   
    public FormPanel getFormPanel() {
        return formPanel;
    }
   
}

你可能感兴趣的:(java,gwt,smartgwt)