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:wubin3347@gmail.com
 *
 */
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);
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值