gwt 前端上传,下载demo
package com.appdev.bsf.vehicletracking.client.fileUpload;
importcom.appdev.bsf.common.client.messagebox.MessageBox;
importcom.google.gwt.core.client.GWT;
importcom.google.gwt.user.client.Window;
importcom.google.gwt.user.client.ui.FileUpload;
importcom.google.gwt.user.client.ui.FormPanel;
importcom.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
importcom.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
importcom.google.gwt.user.client.ui.FormPanel.SubmitEvent;
importcom.google.gwt.user.client.ui.FormPanel.SubmitHandler;
importcom.smartgwt.client.widgets.IButton;
importcom.smartgwt.client.widgets.events.ClickEvent;
importcom.smartgwt.client.widgets.events.ClickHandler;
importcom.smartgwt.client.widgets.layout.HLayout;
/**
*
*/
publicclassDemoGwtFileUploadLayoutextendsHLayout{
privateFormPanelformPanel=null;
publicDemoGwtFileUploadLayout(){
setWidth(200);
finalFileUploadfileUpLoad=newFileUpload();
//名字必须与实体存路径的名字一致
fileUpLoad.setName("imageFileUpload");
formPanel=newFormPanel();
formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
formPanel.setMethod(FormPanel.METHOD_POST);
formPanel.setWidget(fileUpLoad);
finalIButtonbtnUpload=newIButton("upLoadButton");
btnUpload.addClickHandler(newClickHandler(){
@Override
publicvoidonClick(ClickEventevent){
formPanel.submit();
}
});
formPanel.addSubmitHandler(newSubmitHandler(){
@Override
publicvoidonSubmit(SubmitEventevent){
//必须带参数id和clsUrl,参数名必须是id,clsUrl
formPanel.setAction(GWT.getModuleBaseURL()+"fileupload?id=1&clsUrl="+Object.class);
if(fileUpLoad.getFilename().length()==0){
MessageBox.getInstance().initComponents("必须选择一个文件");
event.cancel();
}elseif(!fileUpLoad.getFilename().toLowerCase().endsWith(".jpg")
&&!fileUpLoad.getFilename().toLowerCase().endsWith(".gif")){
event.cancel();
}
}
});
formPanel.addSubmitCompleteHandler(newSubmitCompleteHandler(){
@Override
publicvoidonSubmitComplete(SubmitCompleteEventevent){
Stringresult=event.getResults();
if("".equals(result)){
MessageBox.getInstance().initComponents("文件上传成功");
}else{
MessageBox.getInstance().initComponents("文件上传失败");
}
}
});
addMember(formPanel);
addMember(btnUpload);
}
}
/**
*下载文件
*
*@authorwubinE-Mail:[email protected]
*
*/
classDownLoadLayoutextendsHLayout{
publicDownLoadLayout(){
IButtondownLoad=newIButton("下载",newClickHandler(){
@Override
publicvoidonClick(ClickEventevent){
/**
*必须带参数名,参数名必须为url
*/
Window.open(GWT.getModuleBaseURL()+"filedownload?url=/uploads/2011/10/19/SNV83367.JPG",
"dd","image");
}
});
addMember(downLoad);
}
}