gwt 前端上传,下载demo

package com.appdev.bsf.vehicletracking.client.fileUpload;

import com.appdev.bsf.common.client.messagebox.MessageBox;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
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 DemoGwtFileUploadLayout extends HLayout {
    private FormPanel formPanel = null;

    public DemoGwtFileUploadLayout() {
        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) {
                // 必须带参数id和clsUrl, 参数名必须是id,clsUrl
                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();
                }

            }
        });
        formPanel.addSubmitCompleteHandler(new SubmitCompleteHandler() {

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

        addMember(formPanel);
        addMember(btnUpload);
    }

}

/**
 * 下载文件
 *
 * @author wubin E-Mail:[email protected]
 *
 */
class DownLoadLayout extends HLayout {
    public DownLoadLayout() {
        IButton downLoad = new IButton("下载", new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                /**
                 * 必须带参数名,参数名必须为url
                 */
                Window.open(GWT.getModuleBaseURL() + "filedownload?url=/uploads/2011/10/19/SNV83367.JPG",
                        "dd", "image");
            }
        });
        addMember(downLoad);
    }
}

猜你喜欢

转载自smartgwt.iteye.com/blog/1206939
GWT