强制“保存”;对话框


Forcing "Save As" dialog via jQuery GET

我在下面的test.php文件代码中调用jQuery "GET"。

我试图让脚本弹出一个"另存为"对话框上的结果test.ini文件,以允许它被保存在本地。然而,尽管我可以将结果回显到jQuery fine中,但我似乎无法弹出"另存为"对话框。

更新:多亏了下面的解决方案,我刚刚改变了我的$。Get到window.location.replace.

$('#test').click(
    function()
    {
        //$.get('<?php echo get_bloginfo('template_directory') ?>/test.php');
        window.location.replace("<?php echo get_bloginfo('template_directory') ?>/test.php");
    }
);

你不能得到一个ajax请求来显示一个"另存为"对话框,但是你可以做的是在页面中插入一个隐藏的iframe元素,然后将该iframe的源设置为你想要用户下载的url。瞧,这就是你的另存为。

下面是一个复制粘贴的例子:

$('a#linky').click(function(){
  var iframe = document.createElement("iframe"); 
  iframe.src = 'http://example.com/branding.zip'; 
  iframe.style.display = "none"; 
  document.body.appendChild(iframe);
  return false;
});

您不需要AJAX。直接导航到php然后在php中使用

header('Content-disposition: attachment;filename=whatever.dat');

这将弹出"另存为"对话框,您将保持在原始页面

AJAX请求不能生成文件下载对话框。考虑在一个新窗口中打开下载目标。