download log file txt front end

download log file txt front end

by Thanos Saringelos

JavaScript

var logCreator = (function() {

        var textarea = document.createElement("textarea");
        textarea.id = 'logtextbox';

        var a = document.createElement('a');
        a.download = 'log.txt';  
        a.id = 'downloadlink'; 
        

        function addText(text){
            textarea.value += text;
        }
        
        function downloadLog(text){ 
            a.href = makeTextFile(textarea.value);  
            a.click();
        }
        
        var textFile = null;
        
        function makeTextFile(text) {
            var data = new Blob([text], {
                type: 'text/plain'
            });

            // If we are replacing a previously generated file we need to
            // manually revoke the object URL to avoid memory leaks.
            if (textFile !== null) {
                window.URL.revokeObjectURL(textFile);
            }

            textFile = window.URL.createObjectURL(data);

            return textFile;
        };
        
        return {
        		addText : addText,
            downloadLog : downloadLog
        }
  
    })();
    
    
    logCreator.addText('dsdsd<\nbr>sdsd');
    
    logCreator.downloadLog();