Skip to content Skip to sidebar Skip to footer

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. After click button link to XML come in JS file. JS send link to PHP file. PHP must show save dialog t

Solution 1:

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.


Solution 2:

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. This also means you will need to use the GET method instead of the POST method to transfer the file.

Try this:

JS:

function showAl(url)
{
    window.location.href = '/do_query.php?info=' + url;
}

PHP:

if (isset($_GET['info'])) 
{
    $info = $_GET['info'];

    // ...

This should prompt the user to download the file.


Post a Comment for "How To Save Dynamically XML File From Server To A Local Machine?"