Read local file using JavaScript HTML5 api file (stand-alone website)

I am working on a website that will be packaged in a .exe file. Thus, the site will only be used offline. Now I need to parse the local XML document. How can I get the file descriptor in a local file using the html5 api file?

EDIT: I don't want to use <input...> or drag and drop the file into the browser.

+3
source share
3 answers

I'm afraid that I might be bad news for your design: the action you request explicitly violates the security model, as specified in the file API specification. The client implementation of FileReader () must ensure that "all files that are read by FileReader objects were first selected by the user." (W3C File API, 13. Security Considerations: http://www.w3.org/TR/FileAPI/#security-discussion ).

There would be a big risk for the security of browser scripts that could just randomly open and read any file from the path without any user interaction. No browser manufacturer will provide unlimited access to the entire file system.

If your script really needs this XML file, you will have to instruct users on how to give the browser access to it, since each browser will not allow your code to open it directly without user action.

+12
source

Well, technically this is true, but you are going to (hopefully) bypass the browser security settings, and this is inherently unsafe, but no more than anything else that requires certain 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 kept this vague (and a little unorthodox) for my reasons, but provided that you have the necessary control over the environment, this is entirely possible.

+1
source

I am experiencing the same problem these days. I need a website to display some data every time I launch a web page. The data should be adaptive for each run, automatically, so I don’t think the @Augusto link can solve your question.

After testing various methods (including writing a temporary local XLM or JSON file), I finally convince myself that maybe "replacing" the "data" in the html file might be the easiest way.

I have an html template inside which there is a string like [data]. Each time you start a web page, [data] will be replaced with real data, such as [1,2,3]. So this is a new file that is starting.

You can go to enter a description of the link here to find out how the "replacement" is performed. Good luck.

+1
source

Source: https://habr.com/ru/post/984418/


All Articles