3

I need to read a file from a specific path in the hard drive of my computer using Javascript (or using JQuery, it doesn't matter). I have been searching in Google but the things that I found are not really helpful. The closest thing that I have is:

  function readSingleFile(evt) {
    //Retrieve the first (and only!) File from the FileList object
    var f = evt.target.files[0]; 

    if (f) {
      var r = new FileReader();
      r.onload = function(e) { 
          var contents = e.target.result;
        alert( "Got the file.\n" 
              +"name: " + f.name + "\n"
              +"type: " + f.type + "\n"
              +"size: " + f.size + " bytes \n"
              + "starts with: " + contents.substr(1, contents.indexOf("\n"))
        );  
      }
      r.readAsText(f);
    } else { 
      alert("Failed to load file");
    }
  }

  document.getElementById('fileinput').addEventListener('change', readSingleFile, false);

It allows to select a file using a file chooser, and then it display the contents of the file. I need to do a program that reads a file in which you give the location explicitly, for example c:\files\test.txt, and it prints the contents of the test.txt file.

I Googled this a lot without success, any help is welcome.

5
  • 3
    Why do you think a browser should give you access to my hard disk files?! Commented Nov 7, 2013 at 17:18
  • It appears you are manipulating the File Objcet attached to a form, JS has no way of grabbing flat files on the file system, you would need node for that. Commented Nov 7, 2013 at 17:18
  • I just need a very small application to read some files and display data, so I wanted to make it web, but without web servers to avoid complications, that is why I was thinking on Javascript. So this is not possible? Commented Nov 7, 2013 at 17:18
  • The only way to have access to the file is for the user to manually select it. Commented Nov 7, 2013 at 17:18
  • Thanks for the fast response, I will have to find another language to do the task. Commented Nov 7, 2013 at 17:24

3 Answers 3

9

Someone correct me if I'm wrong but as far as I know this is not possible in JavaScript due to security concerns.

If it were, a web page could grab any file on your file system without your consent or without you knowing. This is a major security concern so I dont believe it is possible.

A user must be given the option to choose a file from their file system knowingly.

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

3 Comments

Thanks for answering, it seem that this is the correct response, I will do the app in another language to avoid problems.
well but its possible in chrome only. Google webkitdirectory
It is possible to read files from a directory in client-side JavaScript using the File System Access API.
0

This is not really possible or recommended for security reasons.

If this is only being used on your personal computer or for testing reasons, you can add an option for your browser to allow file access, but this is obviously a security concern if you're browsing other websites.

Assuming you are using Chrome, you can make a shortcut for chrome and append

--allow-file-access-from-files

to the target field.

Then you'd have to launch that shortcut, and open the html from that browser instance.

Ideally you should host the file server side, or have a file select option.

1 Comment

Thanks for answering, it seem to be more complicated that I thought, so what I'm going to do is to use another language to do this little app.
0

It is not possible to access the users file system.

However with the FileSystem API you can get a temporary or persistent private file system where you can store stuff for you application (cached images for example). You can then combine this API with the File API to allow the user to drag and drop files to your application from the users files.

Depending on you application this may or may not be satisfying.

A tutorial on the FileSystem API: http://www.html5rocks.com/en/tutorials/file/filesystem/

A tutorial on the File API: http://www.sitepoint.com/html5-file-drag-and-drop/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.