简体   繁体   English

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

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

I am working on an web site which will be packed in an .exe file. 我正在一个网站上工作,该网站将打包在.exe文件中。 So the site will only be used offline. 因此该网站将仅在线下使用。 Now i need to parse an local xml document. 现在我需要解析一个本地的xml文档。 How can i get the file handle to a local file using html5 file api? 如何使用html5文件api获取本地文件的文件句柄?

EDIT: I dont want to use <input...> or dragging file into browser. 编辑:我不想使用<input...>或将文件拖入浏览器。

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. 我恐怕我可能成为您设计的坏消息的承担者:您请求的操作明显违反了File API规范中指定的安全模型。 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." FileReader()的客户端实现必须确保“用户首先选择了FileReader对象正在读取的所有文件”。 (W3C File API , 13. Security Considerations: http://www.w3.org/TR/FileAPI/#security-discussion ). (W3C File 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 have to 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文件,那么您将不得不指示用户如何授予浏览器访问权限,因为每个浏览器都会阻止您的代码在没有用户操作的情况下直接打开它。

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. 当然,出于我的原因,我一直保持这种模糊(并且略显不正统),但如果您对环境有必要的控制,那么这是完全可能的。

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]. 我有一个html模板,其中有一个像[data]这样的字符串。 Each time when launch the webpage, [data] will be replaced by real data like [1,2,3]. 每次启动网页时,[数据]将被[1,2,3]等实际数据所取代。 So actually it is the new file that is launched. 实际上它是启动的新文件。

You can go to enter link description here to see how the "replace" is done. 您可以在此处输入链接描述以查看“替换”是如何完成的。 Good luck. 祝好运。

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

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