从文本文件中获取(某些)内容的网页

时间:2015-08-19 20:47:13

标签: html file web lan

我正在尝试在我的本地局域网中的Windows 7 PC上构建一个网页/网站,该网页/网站将从托管网站的同一驱动器上的文本文件中获取一些信息(localhost \ share \ inputfile.txt)

在这个文件中会有以下信息:比尔盖茨先生和他准备好的产品:三星液晶电视以及需要展示的图片名称

在txt文件中只有这三行:

1:比尔盖茨先生 2:三星液晶电视 3:samsungtv.jpg

实际网页应该显示如下内容:

欢迎"比尔盖茨先生"

您的"三星CLD电视"

图片" samsungtv.jpg"

网站,网页等的创建不是问题,而是从文本文件中获取数据的编码。如果文本文件为空,该网站也会显示幻灯片。它当然应该检查每隔几分钟(5左右),如果文本文件中没有任何内容,如果有,则应该引用将使用此信息的特定页面。

挑战任何人? pleeeeeeeeease:)

亲切的问候, 阿兰。

2 个答案:

答案 0 :(得分:0)

这只回答使用客户端脚本语言从文件中提取数据

从文件中获取数据可以在没有服务器端脚本语言的情况下完成,但您必须以某种方式提取和操作该文件的数据。以下是使用JavaScript(jQuery)的示例:

// Make sure the dom is ready
$(function () {
    // Get the contents of the file
    $.get('localhost\share\inputfile.txt', function(data) {
        // If the returned data (as string) is not empty or whitespace
        if (string) {
            // split the string into several strings when a new line occures
            var lines = data.split('\n'); // take note of the \n This is the new line character also known as a line ending, end of line (EOL), or line break.
            // If there are more than 0 lines
            if (lines.length > 0) {
                // For every line console.log out what the line is with the line number prepended.
                for (var i = 0; i < lines.length; i++) {
                    console.log(i + ": " + lines[i]);
                }
            }
        } else {
            // It is empty, do somethin else
            alert('no data present in txt file.');
        }
    });
});

确保在此JavaScript之前包含对jquery的引用

<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>

输出:

0: Mr. Bill Gates
1: Samsung LCD TV
2: samsungtv.jpg

编辑:这是一个完整的工作示例:

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <style type="text/css" media="screen">

        </style>
    </head>
    <body>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $.get('inputfile.txt', function(data) {
                    //If the returned data is not empty or whitespace
                    if (data) {
                        // split the string into several strings when a new line occures
                        var lines = data.split('\n'); // Take note of the \n This is the new line character also known as a line ending, end of line (EOL), or line break.
                        // If there are more than 0 lines
                        if (lines.length > 0) {
                            // For every line console.log out what the line is with the line number prepended.
                            for (var i = 0; i < lines.length; i++) {
                                console.log(i + ": " + lines[i]);
                            }
                        }
                    } else {
                        // It is empty, do somethin else
                        alert('no data present in txt file.');
                    }
                });
            });
        </script>
    </body>
</html>

确保文件inputfile.txt与此文档位于同一文件夹中,并且文本文件包含一些文本行。

答案 1 :(得分:0)

为此你需要javascript,试试这个:

var xml = new XMLHttpRequest(); 
xml.open("GET","PATH HERE",true); 
xml.onreadystatechange = function(){ 
   if(xml.state === 200 && xml.readyState === 4){ 
      var text = xml.response; //this variable will hold the text file content. 
   } 
}
xml.send();