2

Ok, I have a webpage that will run locally, every hour it needs to read a csv "output.csv" and put its contents into a table. The output.csv is generated from a local program automatically. The goal being an internal status board.

The only thing I can't seem to figure out is, without a server or xmlrequst how do I load a plaintext into javascript? (the rest of it should be easy)

This is the code that I've tried:

var reader = new FileReader();
reader.onload = function(event) {
    var contents = event.target.result;
    console.log("File contents: " + contents);
};

reader.onerror = function(event) {
    console.error("File could not be read! Code " + event.target.error.code);
};

reader.readAsText("output.csv");

EDIT:

I've read that using this option "--allow-file-access-from-files" will override the settings and allow me to read local files from chrome. I'm sure there is a way.

9
  • Why you don't want to use AJAX request? Commented Oct 7, 2014 at 11:24
  • Can you use AJAX without a server and grabbing a local file, I thought not. If you could provide an example, maybe using jsfiddle, in the answer section, I'll mark it Commented Oct 7, 2014 at 11:25
  • use File or Blob objects to specify the file or data to read. Commented Oct 7, 2014 at 11:35
  • You cannot instantiate FileReader object directly (var reader = new FileReader()). FileReader objects are obtained indirectly: as a result of a user selecting or from a drag and drop operation Commented Oct 7, 2014 at 11:40
  • @hindmost why cannot instantiate FileReader? var reader = new FileReader() work ok Commented Oct 7, 2014 at 11:50

2 Answers 2

3

Ok, I found an answer!

Javascript:

$(document).ready(function(){

    $.get("output.csv", function( my_var ) {
        alert(my_var)
    });

});

Chrome: "chrome.exe --user-data-dir=c:\temp --allow-file-access-from-files --incognito """ & currentDirectory & "index.html"""

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

Comments

1

The problem is, that you are running JavaScript, that is running in the browser and for security reasons it cannot access any file from your computer. So if you are willing to stick with JavaScript just start a server (e.g. SimpleHttpServer or Node.js) which will supply the file to your code.

Otherwise you will need to ask the user explicitly to "upload" your csv to the webpage - even though it is offline and living in his browser.

2 Comments

I can't start a server, as the machine is locked down. The user is me, but this thing needs to be done completely automated. It's a full screen application running on a large screen. It will be running for months at a time without me even touching it.
The "Security Reasons" can and are disbled, "--allow-file-access-from-files", I have used this flag. Now with the security gone, how do I load the file?

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.