function download () {
// const content = { 'hello': "wor2ld"};
// const fileName = 'sd.json';
// let blob = new Blob(
// [[JSON.stringify(content, null, 2)]],
// { type : 'application/json' });
const fileName = 'sd.xlsx';
let blob = new Blob([s2ab('2asda')], {
// type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
type: "application/vnd.ms-excel"
})
const url = window.URL.createObjectURL(blob);
//URL.createObjectURL(object)表示生成一个File对象或Blob对象
let a = document.createElement("a");
//设置一个隐藏的a标签,href为输出流,设置download
a.style.display = "none";
a.href = url;
// a.setAttribute("download", fileName);
a.download = fileName;
//指示浏览器下载url,而不是导航到它;因此将提示用户将其保存为本地文件
document.body.appendChild(a);
debugger;
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
}
function s2ab(s) {
var buf = new ArrayBuffer(s.length);
var view = new Uint8Array(buf);
for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
return buf;
}
<p onclick="download()">
下载
</p>