通过Web访问更新远程计算机中的文本文件?

时间:2014-06-26 09:10:20

标签: jquery ajax

我有运行带有php的Web服务器的Linux机器。它包含一个文本文件名&#autorun.conf' - 这是一个配置文件,见下文。

用户可以使用某些网络浏览器(例如Google Chrome)从本地计算机上读取该文件(' autorun.conf')。

实际发生的是文件是解析的,用户可以看到当前配置,然后用户可以更新配置,然后将其发送回Linux。

我做了大部分代码: html文件从linux机器读取文件,解析它,显示它并让用户更改它。

例如,以下代码将文件读入'结果'串

function Button1_onclick() {
    console.log("button click");
    $.get('/autorun.conf', 
        function(result) {

更改配置后,最后要做的是发送更新结果'字符串到服务器。

我没看到我怎么能这样做。 我找到了大量的文件上传插件,使我能够通过拖放发送多个文件,并找到简单的例子' form'这使我能够做到。

The simplest version of ajax (jQuery) to upload just one file

但我没有找到一些简单的代码,让我发送更新字符串,使其成为Linux中的文件。

我将很高兴感谢您的帮助。

尼尔

这是html文件,下面是autorun.conf文件

<!DOCTYPE html>
<!DOCTYPE html>
<html>
<head>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>



<script language="javascript" type="text/javascript">
// regular expression
// http://regex101.com/

var autorunFile;

function Button1_onclick() {
    console.log("button click");
    $.get('/autorun.conf', 
        function(result) {
            console.log(typeof(result));
            if (result == 'ON') {
                alert('ON');
            } else if (result == 'OFF') {
                alert('OFF');
            } else {
                // find specified model
                autorunFile = result;

                var pattern = /.*model.*/g;
                var line = pattern.exec(result);
                console.log(line[0]);
                pattern = /\".*\"/g;
                var value = pattern.exec(line[0]);
                console.log(value[0]);
                $("#model").val(value[0]);

                // find ip address
                pattern = /.*app_ip.*/g;
                line = pattern.exec(result);
                console.log(line[0]);
                pattern = /\".*\"/g;
                value = pattern.exec(line[0]);
                console.log(value[0]);
                $("#ipAddress").val(value[0]);

                // find all model options
                // the m flag tell to treat each end of line ( and only the end of file)
                // the g tell to find all matches and not only the first one
                pattern = /(.* =$)/gm;   
                //line = pattern.exec(result);
                line = result.match(pattern);     /// note match give al matches in contrary to exec


                // remove two first entries in array
                var found = $.inArray('stop_programs', line) > -1;
                if (found >= 0) {
                    line.splice(found, 2);
                }

                // trim / chop the ' =' from the end of string
                for( i=0 ; i<line.length ; i++) {
                    line[i] = line[i].substring(0, line[i].length -2);
                }


                var selectList = $("#mySelect");   
                for (k = 0; k < line.length; k++)
                    selectList.append("<option value='" + line[k]+ "'>" + line[k] + "</option>");


            }
        }
    );
}

function changeModel() {
    var val = $( "#mySelect option:selected" ).text();
    console.log(val);
    $("#model").val(val);
}

function Button2_onclick() {
    // extract again the 'model' line
    var pattern = /.*model.*/g;
    var line = pattern.exec(autorunFile);
    //console.log(line[0]);
    var newModel = "model = " + $("#model").val();
    //console.log(newModel);
    var autorunFile1 = autorunFile.replace(line[0], newModel);
    //alert(autorunFile1);

    pattern = /.*app_ip.*/g;
    line = pattern.exec(autorunFile1);
    var appIp = "app_ip ="  + $("#ipAddress").val();
    console.log(appIp);
    var autorunFile2 = autorunFile1.replace(line[0], appIp);

    // now it is the big problem, how can i send 'autorunFile2' string to the linux machine


}



</script>
</head>


<body>
<p>
    <table>
      <tr>
        <td>model</td>
        <td><input id="model"></td>
        <td>ip address</td>
        <td><input id="ipAddress"></td>
      </tr>
      <tr/>
      <tr>
        <td/>
        <td><input id="Button1" type="button" onclick="return Button1_onclick()" value="Get" /></td>
        <td/>
        <td><input id="Button2" type="button" onclick="return Button2_onclick()" value="Set" /></td>
      </tr>
      <tr>
        <td/>
        <td><select id="mySelect" onchange="changeModel()" ></select></td>
       </tr>
     </table>
</p>



<div id="myDiv"> 

</div>
</body>
</html>

这是autorun.conf文件

# Configuration file for AUTORUN

model = "nexus_stated_eth";
app_ip = "192.168.15.110";
debug_mode = "false";
work_dir = "/opt/goji";

stop_programs =
(
    { name = "MBUS_LISTENER"; },
    { name = "ETHERNET_LISTENER"; },
    { name = "GOJIMANAGER"; },
    { name = "HOSTETH_LISTENER"; },
    { name = "BOSSIMX28"; }
);

# words to substitute in program arguments:
substitutions =
(
    { key = "BOARDID"; value = "1"; },
    { key = "PLUGINS_PATH"; value = "/tmp/1"; },
    { key = "TECHNICIAN_SOCKET_PORT"; value = "9979"; },
    { key = "NODE_ID"; value = "10"; }
);


nexus_legacy =
{
  message = "STARTING BOARD AS NEXUS LEGACY";
  programs = 
  (
    { name = "BOSSIMX28"; args = ("-n", "BOARDID", "-f", "PLUGINS_PATH"); },
    { name = "GOJIMANAGER"; args = ("-n", "BOARDID", "-f", "PLUGINS_PATH", "-g", "98", "-s", "6000000", "-d", "/dev/spidev1.0", "-p", "nexus"); },
    { name = "ETHERNET_LISTENER"; args = ("-n", "TECHNICIAN", "-f", "PLUGINS_PATH", "-p", "TECHNICIAN_SOCKET_PORT" , "-i", "eth0"); }
  )
};

nexus_legacy_cmd =
{
  message = "STARTING BOARD AS NEXUS LEGACY CMD";
  programs = 
  (
    { name = "BOSSIMX28"; args = ("-n", "BOARDID", "-f", "PLUGINS_PATH"); },
    { name = "GOJIMANAGER"; args = ("-n", "BOARDID", "-f", "PLUGINS_PATH", "-g", "98", "-s", "6000000", "-d", "/dev/spidev1.0", "-p", "nexus"); }
  )
};

nexus_stated_mbus =
{
  message = "STARTING BOARD AS NEXUS STATED MBUS";
  programs = 
  (
    { name = "BOSSIMX28"; args = ("-n", "BOARDID", "-f", "PLUGINS_PATH"); },
    { name = "MBUS_LISTENER"; args = ("-p", "NODE_ID", "-n", "HOST", "-f", "PLUGINS_PATH", "-d", "/dev/ttyFiq0"); },
    { name = "GOJIMANAGER"; args = ("-n", "BOARDID", "-f", "PLUGINS_PATH", "-g", "98", "-s", "6000000", "-d", "/dev/spidev1.0", "-p", "nexus_stated"); }
  )
};

nexus_stated_eth =
{
  message = "STARTING BOARD AS NEXUS STATED ETH";
  programs = 
  (
    { name = "BOSSIMX28"; args = ("-n", "BOARDID", "-f", "PLUGINS_PATH"); },
    { name = "GOJIMANAGER"; args = ("-n", "BOARDID", "-f", "PLUGINS_PATH", "-g", "98", "-s", "6000000", "-d", "/dev/spidev1.0", "-p", "nexus_stated"); },
    { name = "HOSTETH_LISTENER"; args = ("-n", "HOST", "-f", "PLUGINS_PATH", "-p", "TECHNICIAN_SOCKET_PORT" , "-i", "eth0"); }
  )
};

nexus_stated_usb_rj45adaptor =
{
  message = "STARTING BOARD AS NEXUS STATED USB ETH";
  programs = 
  (
    { name = "BOSSIMX28"; args = ("-n", "BOARDID", "-f", "PLUGINS_PATH"); },
    { name = "GOJIMANAGER"; args = ("-n", "BOARDID", "-f", "PLUGINS_PATH", "-g", "98", "-s", "6000000", "-d", "/dev/spidev1.0", "-p", "nexus_stated"); },
    { name = "HOSTETH_LISTENER"; args = ("-n", "HOST", "-f", "PLUGINS_PATH", "-p", "TECHNICIAN_SOCKET_PORT" , "-i", "eth1"); }
  )
};

nexus_stated_usb_eth =
{
  message = "STARTING BOARD AS NEXUS STATED ETH";
  programs = 
  (
    { name = "BOSSIMX28"; args = ("-n", "BOARDID", "-f", "PLUGINS_PATH"); },
    { name = "GOJIMANAGER"; args = ("-n", "BOARDID", "-f", "PLUGINS_PATH", "-g", "98", "-s", "6000000", "-d", "/dev/spidev1.0", "-p", "nexus_stated"); },
    { name = "HOSTETH_LISTENER"; args = ("-n", "HOST", "-f", "PLUGINS_PATH", "-p", "TECHNICIAN_SOCKET_PORT" , "-i", "usb0"); }
  )
};

2 个答案:

答案 0 :(得分:1)

最后,感谢您的帮助,我找到了解决方案

我展示了两种不同的php解决方案(ajax也差别不大)

第一个解决方案是在我添加的html文件中

// now it is the big problem, how can i send 'autorunFile2' string to the linux machine
    jQuery.ajax({ 
        type: "POST", // The HTTP request method. 
        url: "uploadString.php", // The URL of the data to fetch. 
        data: autorunFile2, // Don't add any data to the URL. 
        dataType: "text", // Execute the response as a script once we get it. 
        success: function() // Call this function when done.
                        {
                            alert("success");
                        }
                    });

这将获取字符串并使用HTTP POST命令发送它

在服务器端,以下php文件获取字符串并将其保存到文件

<?php
file_put_contents('autorun.conf1', file_get_contents('php://input'));
?>

在第二个解决方案中(查看&#39;数据&#39;提交)

// now it is the big problem, how can i send 'autorunFile2' string to the linux machine
jQuery.ajax({ 
    type: "POST", // The HTTP request method. 
    url: "uploadString.php", // The URL of the data to fetch. 
    //data: autorunFile2, // Don't add any data to the URL. 
    data:{post: autorunFile2},
    dataType: "text", // Execute the response as a script once we get it. 
    success: function() // Call this function when done.
                    {
                        alert("success");
                    }
                });

并且服务器中的php是

<?php
$fp = fopen('autorun.conf1', 'w');
$abc =  $_POST['post'];
fwrite($fp, $abc);
?>

感谢

答案 1 :(得分:0)

fwrite ( resource $handle , string $string [, int $length ] )