简体   繁体   English

如何从点击按钮下载服务器上的XML文件?

[英]How to download XML file from server on click button?

I have link to XML file, which locate on the server. 我有指向XML文件的链接,该文件位于服务器上。 When user click button, he must take dialog to save XML file to local disk. 当用户单击按钮时,他必须进行对话框以将XML文件保存到本地磁盘。 I'm determine link to XML file in "a href", but browser opened this file, not save. 我确定在“a href”中链接到XML文件,但是浏览器打开了这个文件,而不是保存。 If i "save link as.." all OK. 如果我“另存为链接..”都OK。 Help me please to solve this problem. 请帮我解决这个问题。

Update: Server - IIS. 更新:服务器 - IIS。 XML files create dynamically. XML文件动态创建。 onClick event i send to js link to my XML file, js POST link to php using ajax. 我将onClick事件发送到js链接到我的XML文件,将js POST链接发送到使用ajax的php。 How modify my php to open "Save Dialog" to save XML file ? 如何修改我的PHP以打开“保存对话框”以保存XML文件? js: 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(do_query.php):

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

The XML file should be served with the HTTP header Content-Disposition: attachment . XML文件应带有HTTP标头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). 当然,还必须适当设置Content-Type标头( application/xml是通用XML mime类型)。 How you add HTTP headers depends on your server configuration: please edit your question to add some details. 添加HTTP标头的方式取决于您的服务器配置:请编辑问题以添加一些详细信息。

In Apache .htaccess , it's 在Apache .htaccess ,它是

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文件,并使该php文件包含类似

<?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. 这应该强制文件被视为下载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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