js 文本 写入文件并下载

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <title>js 文本 写入文件并下载</title>
</head>

<body>
    <button type="button" id="btn">字符串格式转文件下载</button>
    <button type="button" id="btn2">base64格式转文件下载</button>


</body>

<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
    var StringToBtye = {
        stringToBytes(str) {
            var bytes = new Array();
            for (var i = 0; i < str.length; i++) {
                var c = str.charCodeAt(i);
                var s = parseInt(c).toString(2);
                if (c >= parseInt("000080", 16) && c <= parseInt("0007FF", 16)) {
                    var af = "";
                    for (var j = 0; j < (11 - s.length); j++) {
                        af += "0";
                    }
                    af += s;
                    var n1 = parseInt("110" + af.substring(0, 5), 2);
                    var n2 = parseInt("110" + af.substring(5), 2);
                    if (n1 > 127) n1 -= 256;
                    if (n2 > 127) n2 -= 256;
                    bytes.push(n1);
                    bytes.push(n2);
                } else if (c >= parseInt("000800", 16) && c <= parseInt("00FFFF", 16)) {
                    var af = "";
                    for (var j = 0; j < (16 - s.length); j++) {
                        af += "0";
                    }
                    af += s;
                    var n1 = parseInt("1110" + af.substring(0, 4), 2);
                    var n2 = parseInt("10" + af.substring(4, 10), 2);
                    var n3 = parseInt("10" + af.substring(10), 2);
                    if (n1 > 127) n1 -= 256;
                    if (n2 > 127) n2 -= 256;
                    if (n3 > 127) n3 -= 256;
                    bytes.push(n1);
                    bytes.push(n2);
                    bytes.push(n3);
                } else if (c >= parseInt("010000", 16) && c <= parseInt("10FFFF", 16)) {
                    var af = "";
                    for (var j = 0; j < (21 - s.length); j++) {
                        af += "0";
                    }
                    af += s;
                    var n1 = parseInt("11110" + af.substring(0, 3), 2);
                    var n2 = parseInt("10" + af.substring(3, 9), 2);
                    var n3 = parseInt("10" + af.substring(9, 15), 2);
                    var n4 = parseInt("10" + af.substring(15), 2);
                    if (n1 > 127) n1 -= 256;
                    if (n2 > 127) n2 -= 256;
                    if (n3 > 127) n3 -= 256;
                    if (n4 > 127) n4 -= 256;
                    bytes.push(n1);
                    bytes.push(n2);
                    bytes.push(n3);
                    bytes.push(n4);
                } else {
                    bytes.push(c & 0xff);
                }
            }
            return bytes;
        }
    }

    var file = new Uint8Array(StringToBtye.stringToBytes(JSON.stringify({ KEY: '12345678' }))).buffer;
    console.log(file);
    $(function () {
        $("#btn").on('click', function () {
            downloadTxt("hello.txt", "This is the content of my file");
        })
        $("#btn2").on('click', function () {
            downloadByBlob("base64.txt");
        })
    })
    //通过a标签指定文本格式和编码直接下载
    function downloadTxt(fileName, content) {
        let a = document.createElement('a');
        a.href = 'data:text/plain;charset=utf-8,' + content
        a.download = fileName
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
    }
    //通过FileReader转化为base64字符串下载
    function downloadByBlob(fileName) {
        let blob = new Blob([file], {
            type: "text/plain;base64"
        });
        let reader = new FileReader();
        reader.readAsDataURL(blob);
        reader.onload = function (e) {
            let a = document.createElement('a');
            a.download = fileName;
            a.href = e.target.result;
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
        }
    }
</script>

</html>
原文地址:https://www.cnblogs.com/zhenguo-chen/p/13698294.html