简体   繁体   English

用javascript加载和读取本地文本文件的最简单方法?

[英]Easiest way to load and read a local text file with javascript?

I have a .csv file that I wish to load that contains information that the .HTML page will format itself with. 我有一个要加载的.csv文件,其中包含.HTML页将自行格式化的信息。 I'm not sure how to do this however, 我不知道该怎么做,

Here's a simple image of the files: http://i.imgur.com/GHfrgff.png 这是文件的简单图像: http : //i.imgur.com/GHfrgff.png

I have looked into HTML5's FileReader and it seems like it will get the job done but it seems to require usage of input forms. 我已经研究了HTML5的FileReader ,似乎可以完成工作,但似乎需要使用输入表单。 I just want to load the file and be able to access the text inside and manipulate it as I see fit. 我只想加载文件并能够访问其中的文本并按我认为合适的方式进行操作。

This post mentions AJAX, however the thing is that this webpage will only ever be deployed locally, so it's a bit iffy. 这篇文章提到了AJAX,但是实际情况是,该网页只能在本地部署,因此有点麻烦。

How is this usually done? 通常如何做?

Since your web page and data file are in the same directory you can use AJAX to read the data file. 由于您的网页和数据文件位于同一目录中,因此您可以使用AJAX读取数据文件。 However I note from the icons in your image that you are using Chrome. 但是,我从图像中的图标中注意到您正在使用Chrome。 By default Chrome prevents just that feature and reports an access violation. 默认情况下,Chrome仅阻止该功能并报告访问冲突。 To allow the data file to be read you must have invoked Chrome with a command line option --allow-file-access-from-files . 要允许读取数据文件,您必须使用命令行选项--allow-file-access-from-files调用Chrome。

An alternative, which may work for you, is to use drag the file and drop into onto your web page. 一种可能对您有用的替代方法是使用将文件拖放到您的网页上。 Refer to your preferred DOM reference for "drag and drop files". 请参阅首选的DOM参考以获取“拖放文件”。

You can totally make an ajax request to a local file, and get its content back. 您完全可以向本地文件发出ajax请求,然后取回其内容。

If you are using jQuery, take a look at the $.get() function that will return the content of your file in a variable. 如果您使用的是jQuery,请查看$.get()函数,该函数将以变量形式返回文件的内容。 You just to pass the path of your file in parameter, as you would do for querying a "normal" URL. 您只需在参数中传递文件的路径,就像查询“正常” URL一样。

You cannot make cross domain ajax requests for security purposes. 不能出于安全目的发出跨域ajax请求。 That's the whole point of having apis. 那就是拥有api的全部意义。 However you can make an api out of the $.get request URL. 但是,您可以使用$.get请求URL制作一个api。

The solution is to use YQL (Yahoo Query Language) which is a pretty nifty tool for making api calls out of virtually any website. 解决方案是使用YQL(雅虎查询语言),这是一个非常漂亮的工具,可用于从几乎任何网站进行api调用。 So then you can easily read the contents of the file and use it. 因此,您可以轻松读取文件的内容并使用它。

You might want to look at the official documentation and the YQL Console 您可能需要查看官方文档YQL控制台

I also wrote a blog post specifially for using YQL for cross domain ajax requests. 我还写了一篇博客文章,专门针对使用YQL跨域ajax请求。 Hope it helps 希望能帮助到你

You can try AJAX (if you do not need asynchronous processing set "async" to false. This version below ran in any browser I tried when employed via a local web server (the address contains "localhost") and the text file was indeed in the UTF-8-format. If you want to start the page via the file system (the address starts with "file"), then Chrome (and likely Safari, too, but not Firefox) generates the "Origin null is not allowed by Access-Control-Allow-Origin."-error mentioned above. See the discussion here . 您可以尝试AJAX(如果不需要异步处理,请将“ async”设置为false。该版本在我尝试通过本地Web服务器(地址包含“ localhost”)使用的任何浏览器中运行,并且文本文件确实位于如果您想通过文件系统启动页面(地址以“ file”开头),则Chrome(可能还有Safari,但不是Firefox)会生成“ Origin null上面提到的错误。请参阅此处的讨论。

    $.ajax({
        async: false,
        type: "GET",
        url: "./testcsv.csv",
        dataType: "text",
           contentType: "application/x-www-form-urlencoded;charset=UTF-8",
        success: function (data) {
             //parse the file content here
           }
        });

The idea to use script-files which contain the settings as variables mentioned by Phrogz might be a viable option in your scenario, though. 但是,使用脚本文件将设置包含为Phrogz提到的变量的想法在您的方案中可能是可行的选择。 I was using files in the "Ini"-format to be changed by users. 我使用的是“ Ini”格式的文件,供用户更改。

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

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