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/");
Robin Mackenzie
  • 18,801
  • 7
  • 38
  • 56
Wanderer
  • 544
  • 1
  • 7
  • 23
  • 1
    Possible duplicate of [How to set a value to a file input in HTML?](http://stackoverflow.com/questions/1696877/how-to-set-a-value-to-a-file-input-in-html) – Robin Mackenzie Feb 01 '17 at 14:09
  • Use an ajax function? – evolutionxbox Feb 01 '17 at 14:15
  • What environment does this app run in? You probably want to look at using [ServiceWorker API](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API) – charlietfl Feb 01 '17 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. – Wanderer Feb 01 '17 at 14:30

1 Answers1

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");
Community
  • 1
  • 1
jmtalarn
  • 1,513
  • 1
  • 13
  • 16