[英]How to save dynamically XML file from server to a local machine?
Help me please. 请帮帮我。 Where is my mistake ?
我的错误在哪里? I have many XML files on the IIS server.
我在IIS服务器上有很多XML文件。 After click button link to XML come in JS file.
点击按钮后链接到XML进入JS文件。 JS send link to PHP file.
JS发送链接到PHP文件。 PHP must show save dialog to save this link.
PHP必须显示保存对话框以保存此链接。 See code:
看代码:
JS: JS:
function showAl(url)
{
alert(url);
var ajax = getRequest();
ajax.onreadystatechange = function()
{
if(ajax.readyState == 4)
{
...
}
}
ajax.open("POST", "/do_query.php", true);
var data = 'info='+url;
ajax.send(data);
}
PHP: PHP:
<?php
if (isset($_POST['info']))
{
$info = $_POST['info'];
header('Content-Type: application/xml;');
header('Content-Disposition: attachment; filename=file.xml;');
readfile(str_replace(" ", "%20", $info), false);
}
?>
Thank's in advance ! 提前致谢 !
Three simple ways to download a file: 下载文件的三种简单方法:
Good old form 好老形式
<form id="the-form" action="/do_query.php" method="post"> <input type="hidden" name="info" value="test"> <input type="Submit" value="Download with regular form"> </form>
Submit good old form with JavaScript 使用JavaScript提交好的旧表单
<script type="text/javascript"> function download(){ document.getElementById("the-form").submit(); } </script> <input type="Submit" value="Download with JavaScript" onclick="download()">
Switch to GET (requires changes to do_query.php
): 切换到GET(需要更改
do_query.php
):
<a href="/do_query.php?info=test">Download with link</a>
The problem with AJAX is that it runs on current (HTML) page. AJAX的问题在于它在当前(HTML)页面上运行。 It can manipulate the page HTML or redirect to another location but it cannot send a custom HTTP response.
它可以操纵页面HTML或重定向到另一个位置,但它无法发送自定义HTTP响应。
You cannot prompt a user to save a file when you are using AJAX, you will need to direct the browser window to the URL of the file to download. 在使用AJAX时,无法提示用户保存文件,您需要将浏览器窗口指向要下载的文件的URL。 This also means you will need to use the GET method instead of the POST method to transfer the file.
这也意味着您需要使用GET方法而不是POST方法来传输文件。
Try this: 尝试这个:
JS: JS:
function showAl(url)
{
window.location.href = '/do_query.php?info=' + url;
}
PHP: PHP:
if (isset($_GET['info']))
{
$info = $_GET['info'];
// ...
This should prompt the user to download the file. 这应该提示用户下载文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.