简体   繁体   English

从.txt文件加载数组

[英]Loading an array from .txt file

I need to load an array from a .txt file. 我需要从.txt文件加载数组。 It has few strings separated by commas ( , ). 它几乎没有用逗号( , )分隔的字符串。

So far I have found this . 到目前为止,我已经找到了这个

That guy splits it with .split function but I have a problem. 那家伙用.split函数拆分它,但是我有问题。 He uses an input to upload the file, however, I need to link the file to the HTML and than split it into an array and use it. 他使用输入来上传文件,但是,我需要将文件链接到HTML,然后将其拆分为数组并使用它。

Just make sure the text file is accessible through an HTTP request on the same domain and you can read it fine. 只需确保可通过同一域上的HTTP请求访问该文本文件,就可以正常阅读。 Here's an example where the text file is located in the webroot. 这是文本文件位于webroot中的示例。

    $(function(){
        $.get('/whatever.txt', function(data){
            var array = data.split(',');
            console.log(array);
        });
    });

You can read html files locally since html5. 从html5开始,您可以在本地读取html文件。 Check the html5 API and some examples here . 此处检查html5 API和一些示例。 Then use Alexanders answer to parse it. 然后使用亚历山大答案来解析它。

EDIT: here and here are some good and short examples on how to use it. 编辑: 这里这里有一些很好的和简短的示例,说明了如何使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM