如何使用Javascript和HTML 5按名称加载本地文本文件的内容? [英] How to load the contents of a local text file by name using Javascript and HTML 5?
问题描述
在Chrome中工作,加载 本地 html或JS文件。
Working in Chrome, loading a local html or JS file.
我发现了许多如何示例加载使用选择文件输入选择的文件。
I found many examples of how to load a file that is selected using the Choose File input.
然而,没有弄清楚如何操作 给定文件名 ,不使用选择文件输入。
However, didn't figure out how to do it given a file name without using the Choose File input.
选择文件输入返回一个File对象。
The Choose File input returns a File object.
如何在没有选择文件输入的情况下创建File对象?
How to create the File object without the Choose File input?
来自文件API :
new File(
Array parts,
String filename,
BlobPropertyBag properties
);
但是没有弄清楚零件和属性是什么。
But didn't figure out what the parts and properties would be.
编辑:用例:
我有代码覆盖率结果作为测试套件的一部分生成。它存储为JSON(易于阅读),但我需要用源代码显示它。
I have code coverage results generated as part of a test suite. It is stored as JSON (which is easy to read), but I need to display it with the source code.
因此该功能是加载源代码和JSON数据,并使用HTML和Javascript在网页上一起渲染它们。
So the feature is to load the source code and JSON data, and render them together on a web page using HTML and Javascript.
该文件将从浏览器打开并存在于本地计算机上。没有服务器。
The file would be opened from the browser and lives on the local machine. There is no server.
推荐答案
浏览器无法通过名称从文件系统加载任意文件而无需特殊扩展或其他恶作剧。这是一项安全策略,可防止随机网站在您浏览互联网时从您的硬盘中读取文件。
The browser cannot load arbitrary files by name from your filesystem without special extensions or other shenanigans. This is a security policy to prevent random web sites from reading files from your hard disk as you browse the internet.
如果您要做一些特殊的事情,就像您想要编写一个chrome应用程序,你可以访问一些很好的API来访问文件系统:
https ://developer.chrome.com/apps/fileSystem
If you're down to do something special like if you want to write a chrome app, you could get access to some nice APIs for accessing the filesystem: https://developer.chrome.com/apps/fileSystem
这篇关于如何使用Javascript和HTML 5按名称加载本地文本文件的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!