如何将XML文件从服务器动态保存到本地机器? [英] How to save dynamically XML file from server to a local machine?

查看:175
本文介绍了如何将XML文件从服务器动态保存到本地机器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我。我的错误在哪里?我在IIS服务器上有很多XML文件。点击按钮链接到XML后在JS文件中。 JS发送链接到PHP文件。 PHP必须显示保存对话框才能保存此链接。查看代码:

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 
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);
}
?>

提前致谢!

解决方案

下载文件的三种简单方法:


  1. 良好的旧形式

     < form id =the-formaction =/ do_query.phpmethod =post> 
    < input type =hiddenname =infovalue =test>
    < input type =Submitvalue =以常规形式下载>
    < / form>


  2. 使用JavaScript提交优质旧表单

     < script type =text / javascript> 
    函数下载(){
    document.getElementById(the-form)。submit();
    }
    < / script>
    < input type =Submitvalue =使用JavaScript下载onclick =download()>

  3. 切换到GET(需要修改 do_query.php code $):

     < a href =/ do_query.php?info = test>链路< / A> 


AJAX的问题在于,当前(HTML)页面。它可以处理页面HTML或重定向到另一个位置,但不能发送自定义HTTP响应。


Help me please. Where is my mistake ? I have many XML files on the IIS server. After click button link to XML come in JS file. JS send link to PHP file. PHP must show save dialog to save this link. See code:

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
  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:

  1. 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>
    

  2. Submit good old form with JavaScript

    <script type="text/javascript">
    function download(){
        document.getElementById("the-form").submit();
    }
    </script>
    <input type="Submit" value="Download with JavaScript" onclick="download()">
    

  3. Switch to GET (requires changes to 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. It can manipulate the page HTML or redirect to another location but it cannot send a custom HTTP response.

这篇关于如何将XML文件从服务器动态保存到本地机器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆