function download(filename, content, contentType) {
//检查格式,
if (!contentType)
contentType = 'application/octet-stream';
//创建a标签
var a = document.createElement('a');
//创建Blob对象
var blob = new Blob([content], { 'type': contentType });
让a标签指向Blob对象
a.href = window.URL.createObjectURL(blob);
//设置文件名
a.download = filename;
//触发点击事件
a.click();
}
//调用示例
download("data.txt", "hello world");
网友评论