0

I am using javascript and html to load a file (browser).

I currently have this:

<input type="file" id="file-input" @input="openFile" /> 

Then openFile() loads the file using the FileReader API.

My question is, since actually, I want the file to load dynamically - I do not want to have a user prompt for file input. Any way to achieve this? How about for directories (recursively get all matching files and open/display the files in the browser)?

The files I need to load are xml data files and will be parsed for displaying purposes.

These files are located locally (as part of the web page bundle). This web page will be loaded locally - no servers/local serveres whatsoever.

Help please!

Thanks!

5
  • 1
    It's not possible. It wouldn't be secure either. Commented May 31, 2021 at 2:35
  • What do you want to do with these files? If it's jut displaying them, then yes, almost all browsers will allow you to point to files inside the same directory as your current html file. For instance you can do <img src="image.jpg"> if image.jpg is in the same directory as the html file. Commented May 31, 2021 at 2:44
  • If you need to read these files' content by scripts however you'll face a lot of troubles. IIRC Firefox will currently allow you through XHR but Chrome will prevent it and any other way. Commented May 31, 2021 at 2:47
  • Hey @Kaiido - thanks for your replies. The files I need to load (and parse, for displaying purposes) are XML files. Any thoughts or suggestions? Thanks! Commented May 31, 2021 at 2:48
  • The only cross-browser way I can think of would be to embed all your xml files inside the HTML file itself. For binary data you can use data:// URLs, for xml, simple JS strings would do. Well you could obviously also build a complete app with its own localserver running, but that's an other level. Commented May 31, 2021 at 2:52

1 Answer 1

0

It is not possible because of security policies. File input cannot choose element, it allow scammers to steal files from user's computer.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.