~~~
//====================================浏览器点击按钮文件下载====================================
$(document).on('click', '#download_files', function () {
if ('download' in document.createElement('a')) {
if(cluster_datas_str==''){
alert("环境还未启动");
}else {
downloadFile("集群环境配置信息.txt", cluster_datas_str);
}
} else {
alert('浏览器不支持,建议使用最新版谷歌浏览器');
}
});
// 下载文件方法
function downloadFile(filename,content) {
var eleLink = document.createElement('a');
eleLink.download = filename;
eleLink.style.display = 'none';
// 字符内容转变成blob地址
var blob = new Blob([content],{type:'text/plain',endings:"native"});
eleLink.href = URL.createObjectURL(blob);
// 触发点击
document.body.appendChild(eleLink);
eleLink.click();
// 然后移除
document.body.removeChild(eleLink);
};
~~~
### a标签无法下载
~~~
比如txt,png,jpg等这些浏览器支持直接打开的文件是不会执行下载任务的,而是会直接打开文件,
这个时候就需要给a标签添加一个属性“download”;
其中download后面的属性值是下载后文件的文件名字
~~~