1

I have the following code:

function FileHandler() {

}

FileHandler.prototype.open = function(file) {
  var rawFile = new XMLHttpRequest();
  rawFile.open("GET", file, false);
};

When I try to run it in the console, can I pass a local file as an argument where file is? What syntax would I use if I wanted to do that?

6
  • Is this running server side or client side? Commented Jun 10, 2017 at 8:42
  • Just on my local machine, trying to set the contents of a txt file to a variable. Commented Jun 10, 2017 at 8:43
  • You should run this script on something like node.js. If you run this in browser, you will not have access to local files Commented Jun 10, 2017 at 8:44
  • Possible duplicate of Local file access with javascript Commented Jun 10, 2017 at 8:44
  • stackoverflow.com/a/8533057/2892829 Commented Jun 10, 2017 at 8:45

2 Answers 2

0

http://www.creativebloq.com/web-design/read-local-files-file-api-121518548

it shows exactly how you can do that:

The FileReader object allows us to read the content of a file once we've got a file from the user. For security, the user must actively give us the reference to the file in order for it to be readable. There are a number of FileReader methods for getting a file's contents

here is an example of how to use it: (code from the site)

var reader = new FileReader();
//create our FileReader object to read the file

reader.addEventListener("load", fileRead, false);
//add an event listener for the onloaded event

function fileRead(event){
//called when the load event fires on the FileReader

var pictureURL = event.target.result;
//the target of the event is the FileReader object instance
//the result property of the FileReader contains the file contents
}

reader.readAsDataURL(file);
//when the file is loaded, fileRead will be called
Sign up to request clarification or add additional context in comments.

Comments

0

You can use an asynchronous function.

   FileHandler.prototype.open = function(file, callback) {
   var rawFile = new XMLHttpRequest();
   rawFile.open("GET", file, false);
    rawFile.onreadystatechange = function() {
    if (rawFile.readyState == 4) {
        // The request is done; did it work?
        if (rawFile.status == 200) {
            // ***Yes, use `rawFile.responseText` here***
            callback(rawFile.responseText);
        } else {
            // ***No, tell the callback the call failed***
            callback(null);
        }
      }; 
    rawFile.open("GET", file, false);
    rawFile.send();
   };

5 Comments

Thanks, when I am running this in the console in Chrome, how do I pass the actual txt file as an argument? Is this possible? Or can I only do this in node?
FileHandler.open('path-to-file', function(callback){/*callback is now the file*/}))
OK, sorry but what would go in the curly braces?
Anything you want, or nothing at all.
I get an xhr is not defined error when loading it, any idea why that might be?

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.