Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JS read local txt file - html5

    I need my JS code to read a local text file. I found an example where the user selects the file, then javascript displays the content of that file. Things work well. In my case the user does not select the file. How do I hard-code the filename? I tried changing f.name, but it is read-only.


    // Read the specified text file and display it in the <pre> element below
    function readfile(f) {
    var reader = new FileReader(); // Create a FileReader object
    reader.readAsText(f); // Read the file
    reader.onload = function() { // Define an event handler
    var text = reader.result; // This is the file contents
    var out = document.getElementById("text-div"); // Find output element
    out.innerHTML = ""; // Clear it
    out.appendChild(document.createTextNode(text)); // Display file contents
    };
    }

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,065
    Thanks
    36
    Thanked 497 Times in 491 Posts
    Without seeing the rest of your code, I believe that bit would only work in MSIE.

    For a more universal method, consider using AJAX to read text from the server.

  • #3
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jmrker View Post
    Without seeing the rest of your code, I believe that bit would only work in MSIE.

    For a more universal method, consider using AJAX to read text from the server.
    It works with Chrome and with FireFox.

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,277
    Thanks
    10
    Thanked 581 Times in 562 Posts
    the developer cannot set the file name. you can set a user-override-able preference of a file type (mime/ext), but that's it. Not only must the value of the <input> be set, the user must physically click the input in order for JS to read the file contents. This is the same style of run-time permissions used by geolocation, popups, flash clipboard access, etc.

    this is done for security reasons, so google.com can't hard-code c:\\autoexec.bat into a hidden file input and steal your files.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,277
    Thanks
    10
    Thanked 581 Times in 562 Posts
    Quote Originally Posted by jmrker View Post
    Without seeing the rest of your code, I believe that bit would only work in MSIE.
    no, FileReader has been cross-browser from the get go.
    looks like it currently works in about 3 out of every 4 real-world browsers: http://caniuse.com/filereader
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •