【发布时间】:2013-07-17 06:02:12
【问题描述】:
我有一个需要下载 excel 文件的情况。所以我为此使用了 Window.open。问题是我需要在调用 Window.open 之前检查文件是否存在于服务器位置。所以当用户点击下载按钮下面的调用发生时,
public void onClick(Button button, EventObject e) {
final String url = GWT.getModuleBaseURL() + "fileupload/dailyLogReport?param1=param1
openFileDownloadWindow(url,fileName);
}
public void openFileDownloadWindow(final String url,String fileName){
CommonServiceAsync serviceAsyn = CommonService.Util.getInstance();
final AsyncCallback callback = new AsyncCallback() {
public void onSuccess(Object result)
{
isFileExsist = (Boolean)result;
if(isFileExsist){
Window.open( url, "_blank", "status=0,toolbar=0,menubar=0,location=0");
}else{
Window.alert("File not found.");
}
}
public void onFailure(Throwable caught)
{
MessageBox.alert("Error", "Error while getting data"
+ caught.getMessage());
}
};
// calling of the action
serviceAsyn.isDailyLogFileExsists(fileName, callback);
}
但问题是,如果我将 Window.open 放入成功中,它只会打开一个窗口并快速关闭它而无需下载文件。但是,如果我将 Window.open 直接放在 onClick 方法中,它会成功打开弹出窗口并成功下载文件。但是由于我必须通过检查文件是否存在来有条件地下载文件,所以我不能将 Window.open 放在 onClick 中。
Window.open 在回调成功函数中不能正常工作的原因是什么?
【问题讨论】: