首页 > 解决方案 > 如何使用 javascript 读取 txt 或 csv 文件

问题描述

我正在尝试从 Excelsheet 填充数组。我尝试了很多尝试,但在另一篇文章中我得到了部分答案。

CSV is a format more portable than XLS which can be exported from excel. 
This format is easy to be parsed (try to export a CSV file from XLS in excel and open it with a text editor by yourself!). 
If this change in format is acceptable, the problem can be simplified quite a lot.

我在桌面上制作了 .csv 和 .txt 文件夹,但我无法阅读。我尝试了许多 html/js 代码,我从控制台遇到了这个答案。

testexcel.html:43 从源“null”访问“file:///C:/exceltest2.1.txt”处的 XMLHttpRequest 已被 CORS 策略阻止:跨源请求仅支持协议方案:http、数据、铬,铬扩展,铬不受信任,https。

或获取相同的

Fetch API 无法加载 file:///C:/Users/ubi11/Desktop/exceltest2.1.txt。对于 CORS 请求,URL 方案必须是“http”或“https”。getData@testexcel.html:15(匿名)@testexcel.html:12

我试图把我的文件放在谷歌驱动器中:

CORS 策略已阻止从源“null”获取“https://drive.google.com/file/d/1XxRJeH2yOyAmatnhj-zF2CsGz1R3WzQk/view?usp=sharing”的访问权限:否“Access-Control-Allow-Origin” ' 请求的资源上存在标头。如果不透明的响应满足您的需求,请将请求的模式设置为“no-cors”以获取禁用 CORS 的资源。

什么是 CORS 政策?有没有办法读取文件并解析里面的内容?如果没有解决方案,我可以通过 GoogleSheet 做我需要的事情还是会遇到同样的问题?我实际上需要做一些简单的事情,阅读 4 列中的数字列表。我不想把这个列表放在 js 代码和 sql 数据库中。

标签: javascriptcsvcorsphonegaptxt

解决方案


推荐阅读