2

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

我试图让脚本在生成的 test.ini 文件上弹出一个“另存为”对话框,以允许将其保存在本地。然而,虽然我可以将结果回显给 jQuery,但我似乎无法弹出“另存为”对话框。

更新:感谢以下解决方案,我刚刚将 $.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");

    }
);
4

3 回答 3

8

您无法获得显示“另存为”对话框的 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;
});
于 2011-07-14T20:36:41.010 回答
3

为此,您不需要 AJAX。只需导航到有问题的 php 并在该 php 中使用

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

这将弹出“另存为”对话框,您将保留在原始页面上。

于 2011-07-14T20:37:41.920 回答
1

AJAX 请求无法生成文件下载对话框。考虑改为在新窗口中打开您的下载目标。

于 2011-07-14T20:35:12.583 回答