我是 GWT/GAE 的新手。要使用 GAE Blobstore 服务,必须从服务器端返回一个上传 url,如本博客中的教程。
但似乎可以在用户浏览文件时调用此获取上传 url 服务,因此我需要在用户按下选择文件按钮时找到要处理的事件,并在那里设置表单面板的操作属性。
fileupload 本身具有附加/分离或更改事件,但它似乎不是我想要的。
我可以上传到 blobstore 的代码如下: 上传表单是:
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import com.google.gwt.cell.client.DateCell;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.TextArea;
public class UploadFilePanel implements EntryPoint {
private FileServiceAsync upls = GWT.create(FileService.class);
AsyncCallback<String> callback;
FormPanel form;
String upload_url;
public void onModuleLoad() {
get_upload_url();
RootPanel rootPanel = RootPanel.get();
FlowPanel flowPanel = new FlowPanel();
rootPanel.add(flowPanel, 0, 0);
add_submit_form(flowPanel);
}
private void add_submit_form(FlowPanel flowPanel) {
VerticalPanel panel = new VerticalPanel();
flowPanel.add(panel);
// Create a FormPanel and point it at a service.
form = new FormPanel();
panel.add(form);
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setMethod(FormPanel.METHOD_POST);
form.addSubmitCompleteHandler(new SubmitCompleteHandler() {
@Override
public void onSubmitComplete(SubmitCompleteEvent event) {
RootPanel.get().clear();
RootPanel.get().getElement()
.setInnerHTML("upload complete " + event.toString());
}
});
form.setSize("222px", "52px");
// Create a FileUpload widget.
FileUpload upload = new FileUpload();
form.setWidget(upload);
upload.setSize("100%", "100%");
upload.setName("myFile");
TextArea textArea = new TextArea();
panel.add(textArea);
textArea.setWidth("100%");
// Add a 'submit' button.
Button submitbtn = new Button("Submit");
panel.add(submitbtn);
submitbtn.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
upls.get_upload_url("/stmtgen/upload", callback);
}
});
}
private void get_upload_url() {
callback = new AsyncCallback<String>() {
@Override
public void onSuccess(String result) {
form.setAction(result);
upload_url = result;
form.submit();
}
@Override
public void onFailure(Throwable caught) {
RootPanel.get().clear();
RootPanel.get().getElement()
.setInnerHTML("Can't get upload url");
}
};
if (upls == null)
upls = GWT.create(FileService.class);
}
}
我用来获取上传网址的异步服务是这样的:
import com.google.gwt.user.client.rpc.AsyncCallback;
public interface FileServiceAsync {
void get_upload_url(String up, AsyncCallback<String> callback);
}
问题是当用户点击提交按钮时,有两个服务调用会减慢响应速度。
我尝试了 addHandler(Handler, Type) 如下,它不起作用。
FileUpload upload = new FileUpload();
upload.addHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
//get upload url here
}
}, ClickEvent.getType()
);