【问题标题】:Downloading not started after windows.open()windows.open() 后未开始下载
【发布时间】:2018-02-21 16:06:56
【问题描述】:

我有控制器,可以在其中生成和下载文件。 当用户点击“下载”按钮时,执行此代码:

_statusWindow = window.open('downloadCSV', "_statusWindow");
_statusWindow.document.write('<div>  Please wait while we processing your request</div>');
_statusWindow.document.title = "Downloading...";

它指向控制器 downloadCSV,它生成文件(不返回任何视图) 所以新窗口打开,当文件生成时 - 下载自动开始,下载完成后窗口将关闭。 当我没有这一行时,此代码可以正常工作:

_statusWindow.document.write('<div>  Please wait while we processing your request</div>');

但我想附加这一行以向用户显示消息。 有了这一行(正如我在第一个代码 sn-p 中向您展示的那样),只有当我单击按钮 TWICE 时它才会开始下载!我不知道为什么。当我单击一次时 - 它只是显示消息,而不调用控制器并生成文件,当我第二次调用它时 - 它开始下载......

任何建议,请帮助

【问题讨论】:

    标签: javascript java jquery html spring-mvc


    【解决方案1】:

    我怀疑在下载完成之前消息是否停留在窗口上,请你试试下面的代码..

    var win = window.open("","_statusWindow");
    var html = '<html><head></head><body>Please wait while we processing your 
    request...</body></html>';
    var iframe = win.document.createElement('iframe');
    iframe.src = 'data:text/html;charset=utf-8,' + encodeURI(html);
    iframe.onload = window.open("downloadCSV","_statusWindow");
    win.document.body.appendChild(iframe);
    

    【讨论】:

    • 有效!非常感谢 但是我想在下载完成后关闭这个窗口。我试过: $(win.document).ready(win.close());但它会立即关闭窗口。我认为这会起作用,因为当我下载 smth 时,我在浏览器选项卡的左上角有微调器,所以文档还没有准备好。但它不起作用。您对下载完成后如何关闭选项卡有什么建议吗?在我的示例中,下载完成后窗口会自动关闭
    【解决方案2】:

    你能试一试吗,我正在尝试以 iframe 的形式打开下载,并在加载 iframe 时关闭窗口..希望它可以工作..

     var win = window.open("","_statusWindow");
     var html = '<html><head><title>Base</title></head><body>Please wait while 
       we processing your request...</body></html>';
     var iframe = win.document.createElement('iframe');
     iframe.src = 'data:text/html;charset=utf-8,' + encodeURI(html);
     win.document.body.appendChild(iframe);
     var iframe2 = win.document.createElement('iframe');
     iframe2.src = "downloadCSV";
     iframe2.onload= win.close();
     win.document.body.appendChild(iframe2);
    

    【讨论】:

      猜你喜欢
      • 2020-12-01
      • 2011-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多