GWT中的Window.open在回调函数中无法正确打开 [英] Window.open in GWT not open correctly with in a call back function

查看:108
本文介绍了GWT中的Window.open在回调函数中无法正确打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我需要下载一个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(); $ b $ final AsyncCallback callback = new AsyncCallback(){
public void onSuccess(Object result)
{
isFileExsist =(布尔)结果;

if(isFileExsist){
Window.open(url,_blank,status = 0,toolbar = 0,menubar = 0,location = 0);
} else {
Window.alert(File not found。);
}

}
public void onFailure可扔的ca
MessageBox.alert(错误,获取数据时出错
+ caught.getMessage());
}
};
//调用操作
serviceAsyn.isDailyLogFileExsists(fileName,callback);

}

但是问题是如果我把Window.open放在里面它只是打开一个窗口,并快速关闭,无需下载文件即可成功。但是,如果我把Window.open直接放在onClick方法中,它会成功打开窗口并成功下载文件。但由于我必须通过检查文件是否存在来有条件地下载文件,我不能将Window.open放在onClick中。



Window是什么原因。打开在回调成功函数内部不能正常工作?

解决方案

问题是弹出式窗口拦截器。

当您点击某个元素时,您可以打开一个新窗口,因为浏览器认为这是一个故意的用户操作来打开该窗口。否则,浏览器会阻止异步块中的任何window.open,因为它认为它可能是用户控件中的恶意代码。



最好的解决方案是在iframe中打开文件,但必须在服务器端设置适当的content-disposition标题,以便浏览器显示Save对话框。



客户端代码:

  //创建一个新的iframe 
final Frame f = new Frame();
f.setUrl(url_to_my_excel_file);
//设置大小为0px,除非您希望文件显示在其中
//对于.html图像.pdf等,您必须配置你的servlet
//发送Content-Disposition头文件
f.setSize(0px,0px);
RootPanel.get().add(f);
//配置一个定时器从DOM中删除元素b $ b new Timer(){
public void run(){
f.removeFromParent();
}
} .schedule(10000);

服务器代码:

  protected void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException {
[...]
//为您的设置适当的类型文件
resp.setContentType(application / vnd.ms-excel);
//必须如果你想浏览器打开保存对话框
resp.setHeader(Content-Disposition: ,attachment; filename ='my_excel_file.xls');
[...]
}


I have a situation where i need to download a excel file. So i user Window.open for that. The problem is i need to check whether the file is exsist in the server location before call the Window.open. So when user click the download buton below call happens,

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);    

}

But the problem is if i put the Window.open inside the success it just open a Window and getting it close quickly with out download the file. But if i put the Window.open directly in onClick method it successfully open the window pop up and download the file successfully. But Since i have to download the file conditionally by checking whether the file is exists or not I can not put the Window.open inside onClick.

What is the reason Window.open not working properly inside the call back success function?

解决方案

The problem is popup blocker.

When you click on a element you can open a new window since the browser considers it is a deliberate user action to open the window.

Otherwise, the browser blocks any window.open in asynchronous blocks, because it considers that it could be malicious code run out of the user control.

The best solution, is to open the file in an iframe, but you have to set the appropriate content-disposition header in server side which causes the browser to show the "Save" dialog.

Client Code:

  // Create a new iframe
  final Frame f = new Frame();
  f.setUrl(url_to_my_excel_file");
  // Set a size of 0px unless you want the file be displayed in it
  // For .html images .pdf, etc. you must configure your servlet
  // to send the Content-Disposition header
  f.setSize("0px", "0px");
  RootPanel.get().add(f);
  // Configure a timer to remove the element from the DOM
  new Timer() {
    public void run() {
      f.removeFromParent();
    }
  }.schedule(10000);

Server Code:

protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
   [...]
   // Set the appropriate type for your file 
   resp.setContentType("application/vnd.ms-excel");
   // Mandatory if you want the browser open the save dialog
   resp.setHeader("Content-Disposition:", "attachment;filename='my_excel_file.xls'");
   [...]
}

这篇关于GWT中的Window.open在回调函数中无法正确打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆