Upload text file into textarea via Javascript

How to upload a text file in a textarea with Javascript. This script is used in dotmaui.com to upload files completely client-side, to respect the privacy of users.

document.forms['myForm'].elements['myFile'].onchange = function (evt) {
    
    if (!window.FileReader) {
        // Browser is not compatible
        return; 
    }

    var reader = new FileReader();

    reader.onload = function (evt) {
        
        if (evt.target.readyState !== 2)
            return;
        
        if (evt.target.error) {
            alert('Error while reading file');
            return;
        }

        var myText = evt.target.result;
        
        $("#myTextArea").val(myText);

    };

    reader.readAsText(evt.target.files[0]);
};

Leave a Comment

Your email address will not be published. Required fields are marked *