将文件参数直接传递给FileReader

时间:2015-02-19 06:04:08

标签: javascript html text-files filereader

我正在尝试为学校关闭制作一个网页,所以我必须使用javascript写一个文件。我一直在看FileReader,但我找不到一种方法来传递文件的链接,而不是使用输入或任何东西的变量。我是javascript世界中的菜鸟,所以我无法向你描述,但这里是代码:

<head>
        <title>
            Ohio School Closings Home
        </title>
        <style>
            body{
                text-align: center;
            }
        </style>
        <script>

            var reader = new FileReader();
            var closingsFile = reader.readAsText('closings.txt');
            document.getElementById("textContents").innerHTML = closingFile;

            function test(){
                alert("javascipt is working");
            }
        </script>
    </head>
    <body>
        <h1>Ohio School Closings</h1>
        <h4><a href="search.html">Search</a>-<a href="apps.html">App</a>-<a href="admincontrol.html">Admin Control Panel</a></h4>
        <br>
        <h2>Current Closings</h2>
        <p id="textContents">There are currently no closings</p>
        <h2><a href="http:\\www.facebook.com\ohioschoolclosings">Facebook</a></h2>
        <button onClick="test()">Test Javascript<!--Checks for syntax errors temporarilly, will be removed--></button>

1 个答案:

答案 0 :(得分:0)

我决定使用PHP来读取和写入服务器上的文件。


用文件路径替换$ filename

//READ FILE
$file = fopen($filename, "r");
$filecontents = fread($file, filesize($filename));

echo nl2br($filecontents); //if you want to display the text as it would appear in the file