Html 使用 JavaScript HTML5 file api 读取本地文件(离线网站)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18251432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 12:12:08  来源:igfitidea点击:

Read a local file using JavaScript HTML5 file api (offline website)

javascripthtmlfileapi

提问by user1324258

I am working on an web site which will be packed in an .exe file. So the site will only be used offline. Now i need to parse an local xml document. How can i get the file handle to a local file using html5 file api?

我正在一个网站上工作,该网站将打包在一个 .exe 文件中。所以该网站只能离线使用。现在我需要解析本地 xml 文档。如何使用 html5 文件 api 获取本地文件的文件句柄?

EDIT: I dont want to use <input...>or dragging file into browser.

编辑:我不想使用<input...>或将文件拖到浏览器中。

回答by Phil Nicholas

I'm afraid I may be the bearer of bad news for your design: The action you are requesting expressly violates the security model as specified in the File API spec. The client implementation of FileReader() must make sure that "all files that are being read by FileReader objects have first been selected by the user." (W3C File API , 13. Security Considerations: http://www.w3.org/TR/FileAPI/#security-discussion).

恐怕我可能会为您的设计带来坏消息:您明确请求的操作违反了文件 API 规范中指定的安全模型。FileReader() 的客户端实现必须确保“用户首先选择了 FileReader 对象正在读取的所有文件”。(W3C 文件 API,13。安全注意事项:http: //www.w3.org/TR/FileAPI/#security-discussion)。

It would be a huge security risk of browser scripts could just arbitrarily open and read any file from a path without any user interaction. No browser manufacturer would allow unfettered access to the entire file system like that.

浏览器脚本可以随意打开和读取路径中的任何文件而无需任何用户交互,这将是一个巨大的安全风险。没有浏览器制造商会允许像这样不受限制地访问整个文件系统。

If your script really needs that XML file, you are going to haveto instruct users on how to grant the browser access to it, as every browser will prevent your code from opening it directly without user action.

如果您的脚本确实需要该 XML 文件,您将必须指导用户如何授予浏览器访问权限,因为每个浏览器都会阻止您的代码在没有用户操作的情况下直接打开它。

回答by Algorath

Well, technically there is indeed a way, but you're going to (hopefully) need to bypass the browser's security settings, and it is inherently unsafe, but no more so than anything else requiring specific file locations.

好吧,从技术上讲确实有一种方法,但是您将(希望)需要绕过浏览器的安全设置,它本质上是不安全的,但与其他需要特定文件位置的东西相比并不安全。

That said...

那说...

<html>
  <head>
    <script>
      function foo(){
        //insert desired filereading script here.
      }
      document.getElementById("fileFoo").click();
    </script>
  </head>
  <body>
     <input type="file" id="fileFoo" display="hidden" value="filepath.extension" onclick="foo"/>
  </body>
</html>

Naturally, I've kept this vague (and slightly unorthodox) for my reasons, but provided you have the necessary control over the environment, it's completely possible.

自然,出于我的原因,我一直保持这种含糊不清(并且有点不正统),但是如果您对环境有必要的控制权,则完全有可能。

回答by ehfaafzv

I am experiencing the same problem these days. I need the website to display some data each time I launch the webpage. The data need to be adaptive to each launch, automatically, so I don't think @Augusto 's link could solve your question.

这些天我遇到了同样的问题。每次启动网页时,我都需要网站显示一些数据。数据需要自动适应每次启动,所以我认为 @Augusto 的链接不能解决您的问题。

After trying out different ways (including writing a temporary local XLM or JSON file), I finally persuade myself that maybe "replacing" the "data" in the html file could be the most straightforward way.

在尝试了不同的方法(包括编写一个临时的本地 XLM 或 JSON 文件)之后,我终于说服自己,也许“替换”html 文件中的“数据”可能是最直接的方法。

I have a html template, within which there is a string like [data]. Each time when launch the webpage, [data] will be replaced by real data like [1,2,3]. So actually it is the new file that is launched.

我有一个 html 模板,其中有一个像 [data] 这样的字符串。每次启动网页时,[data] 将被替换为 [1,2,3] 等真实数据。所以实际上它是启动的新文件。

You can go to enter link description hereto see how the "replace" is done. Good luck.

您可以在此处输入链接描述以查看“替换”是如何完成的。祝你好运。