Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make a simple file reader using Javascript

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 229
    Comment on it

    Hello Readers, If you developing the website to browse any file and showing its's content on the web page, Then by using Javascript you can do this as follows:-

    function readSingleFile(e) {
      var file = e.target.files[0];
      if (!file) {
        return;
      }
      var reader = new FileReader();
      reader.onload = function(e) {
        var contents = e.target.result;
        displayContents(contents);
      };
      reader.readAsText(file);
    }
    
    function displayContents(contents) {
      var element = document.getElementById('file-content');
      element.innerHTML = contents;
    }
    
    document.getElementById('file-input')
      .addEventListener('change', readSingleFile, false);
    

    And the HTML code will be go like this:-

    <input type="file" id="file-input" />
    <h3>Contents of the file:</h3>
    <pre id="file-content"></pre>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: