-4

can I read the contents of a file from a certain folder using javascript??I have been trying so much research but all gives me the browse but I don't want to browse,I have the file name and folder name, and I want to have the contents of this file only.how could that happen if possible I found this solution,but don't know what is the fs??

function onInitFs(fs) {

fs.root.getFile('log.txt', {}, function(fileEntry) {

// Get a File object representing the file,
// then use FileReader to read its contents.
fileEntry.file(function(file) {
   var reader = new FileReader();

   reader.onloadend = function(e) {
     var txtArea = document.createElement('textarea');
     txtArea.value = this.result;
     document.body.appendChild(txtArea);
   };

   reader.readAsText(file);
 }, errorHandler);

 }, errorHandler);

}

window.requestFileSystem(window.TEMPORARY, 1024*1024, onInitFs, errorHandler);
user3789242
  • 105
  • 2
  • 12

2 Answers2

0

JavaScript runs inside a sandbox that doesn't allow it to read files, you can try with the FileSystem API in HTML5 but that's also considered to become dead as browsers are not interested in supporting it.

mpcabd
  • 1,813
  • 15
  • 20
  • so what do you think is the solution in my project if i want to read a file?? – user3789242 Jul 16 '14 at 09:47
  • Check @gerald-schneider [comment](http://stackoverflow.com/questions/24777201/reading-a-file-from-folder-using-javascript/24777250#comment38449517_24777201) above. – mpcabd Jul 16 '14 at 09:52
0

You can read a file using the latest FileReader API using the following piece of code

    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(file);

WORKING CODE FIDDLE

More about File Reader API:Link

Sunil Hari
  • 1,716
  • 1
  • 12
  • 20