简体   繁体   English

读取文本文件,然后使用JavaScript将内容保存在数组中

[英]read text file and save the content in an array using JavaScript

I've searched a lot about this topic, but couldn't find solution. 我已经搜索了很多有关此主题的内容,但找不到解决方案。 I have a *.txt file containing coordinates (each line has one coordinate) in one column. 我有一个* .txt文件,其中一列包含坐标(每一行都有一个坐标)。 So, I want to read this file from my file system and use those coordinates to visualize the tracking route on Google Maps API v3. 因此,我想从文件系统中读取此文件,并使用这些坐标来可视化Google Maps API v3上的跟踪路线。 I've already visualized the track for three coordinates and it works fine. 我已经可视化了三个坐标的轨迹,并且效果很好。 Can anybody just share a sample code of reading a *.txt file and fill an array with the file content? 谁能分享阅读* .txt文件的示例代码,并用文件内容填充数组? Maybe this is quite easy thing, but I am really stuck on this and would appreciate your help! 也许这是一件很容易的事,但是我真的很坚持,不胜感激! Thanks in advance! 提前致谢!

You can't read a file using javascript. 您无法使用javascript读取文件。 It's a client-side programming language. 它是一种客户端编程语言。 But some developpers have developped a solution. 但是一些开发人员已经开发出解决方案。

Check this link and tell me if it was helpful for you: http://www.htmlgoodies.com/beyond/javascript/read-text-files-using-the-javascript-filereader.html#fbid=iTAoMveIKhq 检查此链接,并告诉我它是否对您有帮助: http : //www.htmlgoodies.com/beyond/javascript/read-text-files-using-the-javascript-filereader.html#fbid=iTAoMveIKhq

I'm not going to share a sample code but you could read the answers to that similar question : 我不会共享示例代码,但是您可以阅读类似问题的答案:

Javascript - read local text file Javascript-读取本地文本文件

Basically loading the txt file with Ajax, and read the responseText attribute of the response. 基本上使用Ajax加载txt文件,并读取responseText属性。

Although it would be easier to read a xml file or a json file, which would be easier to parse than a simple txt file. 尽管读取xml文件或json文件会更容易,但比简单的txt文件更容易解析。

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

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