7

I'm creating an offline javascript app that uses sql.js to load a sqlite database, add some filters and query the data and display various d3 based charts. However my users don't want to be bothered with the database stuff and having database file. I want to load file when the page loads.

<input id="inpLoadDB" type="file" onchange="loadDB()">

When I query that element it returns a FileList Object from which I can get the selected file. How can I build the FileList (or just the file object) without including the HTML element. Something like:

var fl=new FileList();
fl.readDir("./data/");
4
  • 1
    Possible duplicate of How to set a value to a file input in HTML? Commented Feb 1, 2017 at 14:09
  • Use an ajax function? Commented Feb 1, 2017 at 14:15
  • What environment does this app run in? You probably want to look at using ServiceWorker API Commented Feb 1, 2017 at 14:15
  • It runs in local browser. My users prefer IE10 and occasionally Firefox. Their IT department doesn't support Chrome on their computers. There is no web server available (nor can they install one) to host this page. Commented Feb 1, 2017 at 14:30

1 Answer 1

4

Accessing files from browser is restricted. If you're executing it from a browser it requires user interaction like upload file button. Even the File access api for HTML5 requires user to do something to get a file https://www.html5rocks.com/it/features/file_access

Check this other answer on StackOverflow with an update for 2016 https://stackoverflow.com/a/372333/4635829

You can access the filesystem programming with Javascript if you write a Node.js app and use the File I/O module

var fs = require("fs");

// Asynchronous read
fs.readFile('input.txt', function (err, data) {
   if (err) {
      return console.error(err);
   }
   console.log("Asynchronous read: " + data.toString());
});

// Synchronous read
var data = fs.readFileSync('input.txt');
console.log("Synchronous read: " + data.toString());

console.log("Program Ended");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.