使用javascript从解析的文本文件中存储变量

Storing variables from parsed text file using javascript

本文关键字:文件 存储 变量 文本 javascript 使用      更新时间:2023-09-26

文本文件包含我想与我的网站登录目的使用的信息

文本文件每行包含以下内容

用户:密码user2: user2password

我已经尝试使用blob读取文本文件,但变量没有得到存储,所以我想知道我在哪里出错了,或者如果我甚至能够使用此方法读取文件

function readFile(){
    var fileName = txtName.value;
    file = login.txt;
    var blob = file.read();
    var readText = blob.text;
    var lines = data.split("'n && : " );
        //Adding the file content to the label placed
    lblFileContent.setText(readText);
    Ti.API.info(readText);
    // dispose of file handle & blob.
    file = null;
    blob = null;
}

我以前用过这个来读取文本文件:

JavaScript (jQuery):

function readFile() {
    $.post('path/to/php/read_file.php', {
        dir: 'path/to/file',
        file: 'file_name.txt',
    },function(file_contents) {
        if (file_contents != '') {
            //your code here
        }
    });
}
PHP:

<?php
    /* This script is invoked by 'index.html'.
     * This script gets the content of the file that has been changed by 
     * the admin, and uses it to determine the path of the file 
     * that is currently set by the admin.
     */
    $dir = $_POST["dir"];
    $file = $_POST["file"];
    $contents= '';
    if ($dhandle = opendir("$dir")) {
        while (false !== ($entry = readdir($dhandle))) {
            if ($entry != "." && $entry != "..") {
                if ($entry == $file) {
                    $entry_path = $dir.DIRECTORY_SEPARATOR.$entry;
                    $fhandle = fopen($entry_path, 'r');
                    $value = fread($fhandle, filesize($entry_path));
                    fclose($fhandle);
                }
            }
        }
        closedir($dhandle);
        echo "$contents";
    }
?>

你可能想尝试HTML 5文件API

1)要指定文本文件,添加一个输入类型文件HTML标记(如果单击,将显示一个文件选择对话框)。

<input type="file" id="myFileId">

2)添加一个监听器,当你选择一个文件时,它将触发一个'change'事件。

    document.getElementById('myFileId').addEventListener('change', function(e) {
    });

3)在EventListener中使用FileReader读取你的文本文件

    document.getElementById('myFileId').addEventListener('change', function(e) {
        var file = document.getElementById('myFileId').files[0];
        var reader = new FileReader();
        reader.onload = function(e) {
            var userPass = reader.result.split(" "); // split by space
            // userPass is an array where each element has a user:pass 
            // do your stuff

        } // onload
        reader.readAsText(file);
    });

在读取文件之前,您可能需要检查文件类型/大小

这是一个你可能感兴趣的解决方案。

文本文件名为login.txt包含我正在阅读这个文件使用JavaScript。

user
myuser
password
myuserpassword

JavaScript代码

var loadXMLDoc=function(){
    var Credentials={};
    if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    } else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==0){
            var temp=xmlhttp.responseText.split(''n');
            for(i=0;i<temp.length;i++){
                switch(temp[i].trim()){
                    case "user":
                    case "User":
                        Credentials.Username=temp[i+1];
                        break;
                    case "password":
                    case "Password":
                        Credentials.Password=temp[i+1];
                        break;
                }
            }
            console.log(Credentials); //Printing the object in the console.
        }
    }
    xmlhttp.open("GET","login.txt",true);
    xmlhttp.send();
}

HTML input。请注意,我在这里调用了js函数。

<input type="button" id="load" value="Click me!" onclick="loadXMLDoc();"/>

使用function getPassword("myuser")在控制台打印密码

var getPassword=function(username){ // It takes one string parameter which is the username.
    var password="";
    if(Credentials.Username){
        if(Credentials.Username.trim()==username) password=Credentials.Password;
    }
    return password;
};