简体   繁体   中英

How to download XML file from server on click button?

I have link to XML file, which locate on the server. When user click button, he must take dialog to save XML file to local disk. I'm determine link to XML file in "a href", but browser opened this file, not save. If i "save link as.." all OK. Help me please to solve this problem.

Update: Server - IIS. XML files create dynamically. onClick event i send to js link to my XML file, js POST link to php using ajax. How modify my php to open "Save Dialog" to save XML file ? js:

 function funk(url)
    {
    var ajax = getRequest();
    ajax.onreadystatechange = function()
    {
        if(ajax.readyState == 4)
        {
         ...
        }
    }
    ajax.open("POST", "/do_query.php", true);
    ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    var data = 'info='+url;
    ajax.send(data);
    }

php (do_query.php):

<?php
  if (isset($_POST['info'])) 
  {
    $info = $_POST['info'];
  }
?>

The XML file should be served with the HTTP header Content-Disposition: attachment . This tells the browser to download the file instead of opening it. Of course, the Content-Type header must also be set appropriately ( application/xml is the generic XML mime type). How you add HTTP headers depends on your server configuration: please edit your question to add some details.

In Apache .htaccess , it's

Header set Content-Disposition attachment

In other servers, it'll vary.

Easiest way would be to make a php file that you link to, and have that php file contain something like

<?php
header('Content-Type: application/xml;');
header('Content-Disposition: attachment; filename=blah.xml;');
readfile('blah.xml');

that should force the file to be seen as a download.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM