function downloadFile(fileName, content){ var aLink = document.createElement('a'); var blob = new Blob([content]); var evt = document.createEvent("HTMLEvents"); // initEvent 不加后两个参数在FF下会报错, 感谢 Barret Lee 的反馈 evt.initEvent("click", false, false); aLink.download = fileName; aLink.href = URL.createObjectURL(blob); aLink.dispatchEvent(evt); }
ps: 兼容性还存在问题, Chrome直接可用, 要兼容FF和IE还需调整.