-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);
0

2 Answers 2

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.

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

2 Comments

so what do you think is the solution in my project if i want to read a file??
Check @gerald-schneider comment above.
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

1 Comment

as I said earlier i dont want to browse for the file, i already have the file name

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.