html部分:
<button type="button">点我下载</button>
js部分:
document.querySelector(‘button‘).onclick = function(){ var records = "这里是要下载的内容"; downloadFn(records,‘下载.txt‘) } function downloadFn (content,filename){ var blob; var eleLink = document.createElement(‘a‘); if(‘download‘ in eleLink){ eleLink.download = filename; eleLink.style.display = ‘none‘; blob = new Blob([content]); eleLink.href = window.URL.createObjectURL(blob); document.body.appendChild(eleLink); eleLink.click(); document.body.removeChild(eleLink) }else{ //浏览器兼容 blob = new Blob([content]); window.navigator.msSaveOrOpenBlob(blob,filename) } }
如果是json格式,可以去进行一下格式化,以便在文本查看。