js生成txt文件

时间:2022-10-29 17:22:28

HTML CODE:

<div class="modal-footer">
     <a onfocus="this.blur();" id="createInvoteBtn" class="ipt-todo" href="javascript:void(0)">生成并导出Txt文件</a>
     <a onfocus="this.blur();" download="code.txt" id="createInvote" class="ipt-todo hide" >code</a>
</div>

 

Js CODE:

var isIE = (navigator.userAgent.indexOf('MSIE') >= 0);
if (isIE) {
     var strHTML = _txt;
     var winSave = window.open();
     winSave.document.open("text","utf-8");
     winSave.document.write(strHTML);
     winSave.document.execCommand("SaveAs",true,"code.txt");
     winSave.close();
} else {
     var elHtml = _txt;
     var mimeType =  'text/plain';
     $('#createInvote').attr('href', 'data:' + mimeType  +  ';charset=utf-8,' + encodeURIComponent(elHtml));
     document.getElementById('createInvote').click();
}

 

效果:

js生成txt文件    js生成txt文件