DownloadFile.js

angular.module('homepage').
factory('DownloadFile', function () {
    function getPlainText(filename, content) {
        var filetype = 'plain/text';
        var hrefContent = 'data:' + filetype + ';charset=utf-8,' + encodeURIComponent(content);
        downloadLink(filename, hrefContent);
    }

    function getDocxFile(filename, content) {
        const docxMimeType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
        var blob = new Blob([content], {'type': docxMimeType});
        var hrefContent = URL.createObjectURL(blob);

        downloadLink(filename, hrefContent);
    }

    function downloadLink(filename, hrefContent) {
        var element = document.createElement('a');
        element.setAttribute('href', hrefContent);
        element.setAttribute('download', filename);

        element.style.display = 'none';
        document.body.appendChild(element);

        element.click();

        document.body.removeChild(element);
    }

    return {
        getPlainText: getPlainText,
        getDocxFile: getDocxFile
    };
});