前端下载

假设某个网页源码中有个表格

<html><head><meta charset="UTF-8"></head>
<body><table>
<tbody><tr><td>姓名</td><td>年龄</td><td>性别</td></tr>
<tr><td>畅心</td><td>30</td><td>男</td></tr>
<tr><td>象山海鲜</td><td>32</td><td>男</td></tr>
</tbody></table></body></html>

我们需要以文档形式下载怎么处理?
假设网页源码字符定义为x,那么使用:

Binary.ToText(Text.ToBinary(x))

将返回

PGh0bWw+PGhlYWQ+PG1ldGEgY2hhcnNldD0iVVRGLTgiPjwvaGVhZD4NCjxib2R5Pjx0YWJsZT4NCjx0Ym9keT48dHI+PHRkPuWnk+WQjTwvdGQ+PHRkPuW5tOm+hDwvdGQ+PHRkPuaAp+WIqzwvdGQ+PC90cj4NCjx0cj48dGQ+55WF5b+DPC90ZD48dGQ+MzA8L3RkPjx0ZD7nlLc8L3RkPjwvdHI+DQo8dHI+PHRkPuixoeWxsea1t+mynDwvdGQ+PHRkPjMyPC90ZD48dGQ+55S3PC90ZD48L3RyPg0KPC90Ym9keT48L3RhYmxlPjwvYm9keT48L2h0bWw+

此时在返回的字符串前面拼接一个前缀:

data:application/vnd.ms-excel;base64,

组合成:

data:application/vnd.ms-excel;base64,PGh0bWw+PGhlYWQ+PG1ldGEgY2hhcnNldD0iVVRGLTgiPjwvaGVhZD4NCjxib2R5Pjx0YWJsZT4NCjx0Ym9keT48dHI+PHRkPuWnk+WQjTwvdGQ+PHRkPuW5tOm+hDwvdGQ+PHRkPuaAp+WIqzwvdGQ+PC90cj4NCjx0cj48dGQ+55WF5b+DPC90ZD48dGQ+MzA8L3RkPjx0ZD7nlLc8L3RkPjwvdHI+DQo8dHI+PHRkPuixoeWxsea1t+mynDwvdGQ+PHRkPjMyPC90ZD48dGQ+55S3PC90ZD48L3RyPg0KPC90Ym9keT48L3RhYmxlPjwvYm9keT48L2h0bWw+

将上面的组合代码直接粘贴到浏览器地址栏回车看看发生了什么?
该题灵感来源于最近网友需要下载网页表格文档,于是有一段js脚本:

<html>    
    <body>
        <table id="扯淡">
            <tr><td>姓名</td><td>年龄</td><td>性别</td></tr>
            <tr><td>畅心</td><td>30</td><td>男</td></tr>
            <tr><td>象山海鲜</td><td>32</td><td>男</td></tr>
        </table>
    </body>
<script>
var tb=document.getElementById('扯淡').innerHTML;
var template =`<html><head><meta charset="UTF-8"></head><body><table>${tb}</table></body></html>`
window.location.href = 'data:application/vnd.ms-excel;base64,' + btoa(unescape(encodeURIComponent(template)));
</script>
</html>

另存为html用IE打开,该死的IE就是不同意下载,要下载你可以使用ActiveXObject配合,大家可以百度一下也很简单!
file

道高一尺 魔高一丈
https://pbihub.cn/users/44