2017-09-13 38 views
0

我有我生成和下载文件的控制器。 当用户点击“下载”按钮,该代码被执行:下载未开始后windows.open()

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

但我想追加这条线,以显示该消息的用户。 使用此行(正如我在第一个代码段中展示的那样),它仅在单击按钮TWICE时开始下载!我不知道为什么。当我点击一次 - 它只是显示信息,而无需呼叫控制器和生成的文件,当我称它为第二次 - 它开始下载...

任何建议,请大家帮忙

回答

1

你可以试试看,我试图打开下载的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); 
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); 
+0

它的工作原理!非常感谢你 但是我想关闭这个窗口,下载完成后。我试过: $(win.document).ready(win.close()); 但它立即关闭窗口。我认为这会起作用,因为当我下载smth时,我在浏览器选项卡的左上角有微调,所以文档还没有准备好。 但它不起作用。下载完成后,您有任何建议如何关闭该选项卡吗?在我的例子中,下载完成后窗口自动关闭 – Bilberryfm