Skip to content Skip to sidebar Skip to footer

How To Load The Contents Of A Local Text File By Name Using Javascript And Html 5?

Working in Chrome, loading a local html or JS file. I found many examples of how to load a file that is selected using the Choose File input. However, didn't figure out how to do i

Solution 1:

The browser cannot load arbitrary files by name from your filesystem without special extensions or other shenanigans. This is a security policy to prevent random web sites from reading files from your hard disk as you browse the internet.

If you're down to do something special like if you want to write a chrome app, you could get access to some nice APIs for accessing the filesystem: https://developer.chrome.com/apps/fileSystem

Solution 2:

The File constructor doesn't read a file from the harddrive, but rater make a virtual file, consider this:

var file = newFile(["some", "content"], "/tmp/my-name.txt");
var reader = newFileReader();
reader.onload = function() {
  console.log(reader.result); // somecontent
};

No file will be read or stored on the clients machine.

If you are talking about creating files in then you should take a look at fs.

Solution 3:

For security reasons all browsers don't support predefined values on file fields so the answer is you can't.

Post a Comment for "How To Load The Contents Of A Local Text File By Name Using Javascript And Html 5?"