强制“另存为”通过 jQuery GET 的对话框
我在下面的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法获得 ajax 请求来显示“另存为”对话框,但您可以做的是在页面中插入一个隐藏的 iframe 元素,然后将该 iframe 的源设置为您希望用户下载的 url。瞧,这就是你的另存为。
这是一个复制和粘贴的示例:
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:
为此,您不需要 AJAX。只需导航到有问题的 php,然后在该 php use 中,
这将弹出“另存为”对话框,您将保留在原始页面上。
You don't need an AJAX for this. Just navigate to the php in question and in that php use
This will pop-up the "save as" dialogue box and you'll remain on the original page.
AJAX 请求无法生成文件下载对话框。考虑在新窗口中打开您的下载目标。
An AJAX request can't spawn the file download dialog. Consider instead opening your download target in a new window.