1

I am new to javascript and want to load the file without having to click on the load file button

I am using the following script and I want the text to be loaded automatically in the text box.

<html>
    <body>
        <table>
            <tr>
                <td>Select a File to Load:</td>
                <td>
                    <input type="file" id="fileToLoad">
                </td>
                <td>
                    <button onclick="loadFileAsText()">Load File</button>
                <td>
            </tr>
            <tr>
                <td colspan="3">
                    <textarea id="inputTextToSave" style="width:512px;height:256px"></textarea>
                </td></tr>
            </tr>
        </table>
    </body>
</html>

<script type='text/javascript'>
function loadFileAsText()
{
    var fileToLoad = document.getElementById("fileToLoad").files[0];
    var fileReader = new FileReader();
    fileReader.onload = function(fileLoadedEvent) 
    {
        var textFromFileLoaded = fileLoadedEvent.target.result;
        document.getElementById("inputTextToSave").value = textFromFileLoaded;
    };
    fileReader.readAsText(fileToLoad, "UTF-8");
}
</script>

Thanks in advance for the help.

4 Answers 4

3

Try adding the onchange attribute, this will call functions when changes have been made to that input.

<input type="file" id="fileToLoad" onchange="loadFileAsText()">

Demo

function loadFileAsText(){
var fileToLoad = document.getElementById("fileToLoad").files[0];
var fileReader = new FileReader();
    fileReader.onload = function(fileLoadedEvent){ 
        document.getElementById("inputTextToSave").value = fileLoadedEvent.target.result;
    };
fileReader.readAsText(fileToLoad, "UTF-8");
}
<table><tr>
<td>Select a File to Load:</td>
<td><input type="file" id="fileToLoad" onchange="loadFileAsText()"></td>
                                 <!-- ^^ onchange attribute added ^^ -->
</tr><tr>
<td colspan="2"><textarea id="inputTextToSave" style="width:512px;height:256px"></textarea></td>
</tr></table>

If you have any questions about the above source code please leave a comment below and I will get back to you as soon as possible.

I hope this helps. Happy coding!

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

1 Comment

Absolutely Great. Thanks a lot... :)
0

You can just call the function on it's own like this:

loadFileAsText();

6 Comments

Thanks for the comment. However, I still don't know what to do. Can you modify the code...
Just add this to your script file window.onload = loadFileAsText;
Thanks. @TurboTech. I am sure something would have to be removed from the code as well.. I am not sure
Why would you want to run the function once the page has loaded? The input will be empty since the client hasn't selected a file...
Not sure what that means @NewToJS . The client has selected a file by choosing the file by pressing "Choose file" button. I am trying to load an existing file, and then modify the file after getting some inputs through 6 radio buttons. Now the code contains a "Load button" for loading the file which I want to eliminate.
|
0

You can do it using Javascript events. for ex: you can call like below:

<input type="file" id="fileToLoad" onblur="loadFileAsText()">

6 Comments

Thanks for the response @Manju. Since I am new to javascript, how would the exact code look like. If you can help...
I just went through your code, basically you are trying to read file data and display it. however the loadFileAsText() function is not doing that yet.
check the exact code which i put in jsfiddle.net/jage0e48/1 to read file data and display it
Thanks for the understanding. Possibly you can help too.
you can read more about file operations in html5rocks.com/en/tutorials/file/dndfiles and if the above code worked for you, can you please mark it as answered?
|
0

Respond to the document's ready event:

$(document).ready( loadFileAsText );

If you don't want to use jQuery for simple compatibility with multiple browsers, then see this answer: pure JavaScript equivalent to jQuery's $.ready() how to call a function when the page/dom is ready for it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.