JS 文本 写入文件并下载




  1 <!DOCTYPE html>
  2 <html lang="zh-CN">
  3 
  4 <head>
  5     <meta charset="UTF-8">
  6     <meta http-equiv="X-UA-Compatible" content="IE=Edge">
  7     <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  8     <title>js 文本 写入文件并下载</title>
  9 </head>
 10 
 11 <body>
 12     <button type="button" id="btn">字符串格式转文件下载</button>
 13     <button type="button" id="btn2">base64格式转文件下载</button>
 14 
 15 
 16 </body>
 17 
 18 <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
 19 <script>
 20     var StringToBtye = {
 21         stringToBytes(str) {
 22             var bytes = new Array();
 23             for (var i = 0; i < str.length; i++) {
 24                 var c = str.charCodeAt(i);
 25                 var s = parseInt(c).toString(2);
 26                 if (c >= parseInt("000080", 16) && c <= parseInt("0007FF", 16)) {
 27                     var af = "";
 28                     for (var j = 0; j < (11 - s.length); j++) {
 29                         af += "0";
 30                     }
 31                     af += s;
 32                     var n1 = parseInt("110" + af.substring(0, 5), 2);
 33                     var n2 = parseInt("110" + af.substring(5), 2);
 34                     if (n1 > 127) n1 -= 256;
 35                     if (n2 > 127) n2 -= 256;
 36                     bytes.push(n1);
 37                     bytes.push(n2);
 38                 } else if (c >= parseInt("000800", 16) && c <= parseInt("00FFFF", 16)) {
 39                     var af = "";
 40                     for (var j = 0; j < (16 - s.length); j++) {
 41                         af += "0";
 42                     }
 43                     af += s;
 44                     var n1 = parseInt("1110" + af.substring(0, 4), 2);
 45                     var n2 = parseInt("10" + af.substring(4, 10), 2);
 46                     var n3 = parseInt("10" + af.substring(10), 2);
 47                     if (n1 > 127) n1 -= 256;
 48                     if (n2 > 127) n2 -= 256;
 49                     if (n3 > 127) n3 -= 256;
 50                     bytes.push(n1);
 51                     bytes.push(n2);
 52                     bytes.push(n3);
 53                 } else if (c >= parseInt("010000", 16) && c <= parseInt("10FFFF", 16)) {
 54                     var af = "";
 55                     for (var j = 0; j < (21 - s.length); j++) {
 56                         af += "0";
 57                     }
 58                     af += s;
 59                     var n1 = parseInt("11110" + af.substring(0, 3), 2);
 60                     var n2 = parseInt("10" + af.substring(3, 9), 2);
 61                     var n3 = parseInt("10" + af.substring(9, 15), 2);
 62                     var n4 = parseInt("10" + af.substring(15), 2);
 63                     if (n1 > 127) n1 -= 256;
 64                     if (n2 > 127) n2 -= 256;
 65                     if (n3 > 127) n3 -= 256;
 66                     if (n4 > 127) n4 -= 256;
 67                     bytes.push(n1);
 68                     bytes.push(n2);
 69                     bytes.push(n3);
 70                     bytes.push(n4);
 71                 } else {
 72                     bytes.push(c & 0xff);
 73                 }
 74             }
 75             return bytes;
 76         }
 77     }
 78 
 79     var file = new Uint8Array(StringToBtye.stringToBytes(JSON.stringify({ KEY: '12345678' }))).buffer;
 80     console.log(file);
 81     $(function () {
 82         $("#btn").on('click', function () {
 83             downloadTxt("hello.txt", "This is the content of my file");
 84         })
 85         $("#btn2").on('click', function () {
 86             downloadByBlob("base64.txt");
 87         })
 88     })
 89     //通过a标签指定文本格式和编码直接下载
 90     function downloadTxt(fileName, content) {
 91         let a = document.createElement('a');
 92         a.href = 'data:text/plain;charset=utf-8,' + content
 93         a.download = fileName
 94         document.body.appendChild(a);
 95         a.click();
 96         document.body.removeChild(a);
 97     }
 98     //通过FileReader转化为base64字符串下载
 99     function downloadByBlob(fileName) {
100         let blob = new Blob([file], {
101             type: "text/plain;base64"
102         });
103         let reader = new FileReader();
104         reader.readAsDataURL(blob);
105         reader.onload = function (e) {
106             let a = document.createElement('a');
107             a.download = fileName;
108             a.href = e.target.result;
109             document.body.appendChild(a);
110             a.click();
111             document.body.removeChild(a);
112         }
113     }
114 </script>
115 
116 </html>
为人:谦逊、激情、博学、审问、慎思、明辨、 笃行
学问:纸上得来终觉浅,绝知此事要躬行
为事:工欲善其事,必先利其器。
态度:道阻且长,行则将至;行而不辍,未来可期
转载请标注出处!
原文地址:https://www.cnblogs.com/ios9/p/14765012.html