加载中...

前端通过字符串生成并下载txt文件

ini
复制代码
//通过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, content) { let blob = new Blob([content], { type: "text/plain;charset=utf-8" }); 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); } } 我用的是A链接下载 $.ajax({ url: addressURL+''/cegtClass/queryErrorInfo'', data:{ confirmKey: resData, }, type: ''post'', success:function(res){ var str=''<a >查看详细日志信息</a>'' $(".uploadSuccces").append(str); $(".downLoadTxt").on(''click'', function () { downloadTxt("查看详细日志信息.txt",res); }) }, error:function(){ } })