简体   繁体   中英

How to read a local file using HTML5 file API using path only

I have a local HTML file and I want to load multiple text files similar to how an include file would work. HTML5 file API will do what I want except for the fact that I have to pick the files via a dialog box. The external file paths will all be known already and I don't want to have the user have to select the files each time reports are run.

Is there a way to bypass the file selection portion of the API? The file selection returns an object and I've tried stringifying it, editing in the path (by using filename, ./filename, and full filepath), then JSON.parsing it to try to restore its original state but I couldn't get that to work.

I've also tried:

  • Embed and object but the contents are not available via DOM.
  • Linked JavaScript file but that requires editing the original to manage line breaks.
  • jQuery .load and .get but I can't get it to work on local files.

The HTML5 API will work fine, I just don't know how to force feed it a local document path instead of having to specify it by dialog or drag/drop.

Did some more research and it sounds like it is not possible to simply supply a path due to security reasons. Makes sense though it's frustrating to not be able to do the equivalent of a server side include on the client side since it's the same domain and all. If someone is going to open up a dangerous file on their own machine then it's game over already.

So for the time being I'm going to save the files as .js then put in line ends and escape quotes to get the content into the parent file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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