Load a file automatically without using a click button

前端 未结 4 1844
走了就别回头了
走了就别回头了 2021-01-16 07:15

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 aut

4条回答
  •  青春惊慌失措
    2021-01-16 08:00

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

    
    

    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");
    }
    Select a File to Load:

    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!

提交回复
热议问题