读书人

gwt 前端下传组件

发布时间: 2012-07-16 15:44:59 作者: rapoo

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,<br>
??? ?*??????????? 设置action,例如:formPanel.setAction(GWT.getModuleBaseURL() +"fileupload?id=1&clsUrl=" + Object.class);<br>
??? ?*??????????? //id:为当前记录的id值,clsUrl:当前操作实体类全路径(注意参数名字必须一样)<br>
??? ?*??????????? if (fileUpLoad.getFilename().length() == 0) {<br>
??? ?*??? ??? ??? ??? MessageBox.getInstance().initComponents("必须选择一个文件");<br>
??? ?*??? ??? ??? ??? event.cancel();<br>
??? ?*??? ??? ??? } else if (!fileUpLoad.getFilename().toLowerCase().endsWith(".jpg")<br>
??? ?*??? ??? ??? ??? ??? && !fileUpLoad.getFilename().toLowerCase().endsWith(".gif")) {<br>
??? ?*??? ??? ??? ??? event.cancel();<br>
??? ?*??? ??? ??? }//验证<br>
??? ?* @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;
??? }
???
}

读书人网 >软件架构设计

热点推荐