强制“另存为”通过 jQuery GET 的对话框

发布于 2024-11-24 13:16:56 字数 455 浏览 4 评论 0原文

我在下面的 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");

    }
);

I'm calling a jQuery "GET" on the test.php file code below.

I'm trying to get the script to pop a "Save As" dialog on the resulting test.ini file to allow it to be saved locally. However, although I can echo the result back to the jQuery fine, I can't seem to pop the "save as" dialog.

Update: Thanks to the solutions below, I just changed my $.get to a 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");

    }
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

星星的軌跡 2024-12-01 13:16:56

您无法获得 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;
});

You can't get an ajax request to show a "Save As" dialog, but what you CAN do is insert a hidden iframe element in the page, then set the source of that iframe to the url you want the user to download. Voila, there's your Save As.

Here's a copy and paste example:

$('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;
});
厌味 2024-12-01 13:16:56

为此,您不需要 AJAX。只需导航到有问题的 php,然后在该 php use 中,

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

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

You don't need an AJAX for this. Just navigate to the php in question and in that php use

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

This will pop-up the "save as" dialogue box and you'll remain on the original page.

静赏你的温柔 2024-12-01 13:16:56

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

An AJAX request can't spawn the file download dialog. Consider instead opening your download target in a new window.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文