简体   繁体   English

如何将XML文件从服务器动态保存到本地计算机?

[英]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: 下载文件的三种简单方法:

  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 使用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 ): 切换到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.

相关问题 如何将file_exist从服务器生成到本地机器? - How to make a file_exist from a server to the local machine? 如何将文件从Sftp服务器下载到本地计算机 - How to download file from Sftp server to local machine 如何从本地计算机上载文件并将其移动到Joomla 3.6的特定目录(从config.xml上载) - How to upload file from local machine and move it into a specific directory of Joomla 3.6 (upload from config.xml) 如何从存储在本地计算机中的 javascript 文件查询存储在本地主机服务器中的 php 文件? - How to query a php file stored in localhost server from a javascript file stored in local machine? .mdb文件中的希腊字符显示在本地计算机上,而不显示在Web服务器上 - Greek characters from .mdb file shown on local machine but not on web server 无法使用php函数将文件从本地计算机上传到服务器 - Unable to upload the file from local machine to server using php function 如何使用Haxe PHP在服务器上保存XML文件? - How to save an XML file on server with Haxe PHP? 如何在PHP中将XML文件保存到本地文件系统(客户端)? - How to save an XML file to local file system (client) in PHP? 使用php将csv文件保存在本地计算机上 - save csv file on local machine using php 如何使用ftp从服务器下载文件以及如何将该文件保存在本地文件夹中 - How to download a file from server using ftp and how to save that file in my local folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM