/**
 * base64数据导出文件，文件下载
 * @param {String} filename 导出文件名称
 * @param {String} href a 标签的 href 属性值
 */
export const downloadFile = function(filename, href) {
    const DownloadLink = document.createElement('a');
    if (DownloadLink) {
        document.body.appendChild(DownloadLink);
        DownloadLink.style = 'display: none';
        DownloadLink.download = filename;
        DownloadLink.href = href;

        // 兼容浏览器底层事件不一致
        if (document.createEvent) {
            let DownloadEvt = document.createEvent('MouseEvents');
            DownloadEvt.initEvent('click', true, false);
            DownloadLink.dispatchEvent(DownloadEvt);
        } else if (document.createEventObject) {
            DownloadLink.fireEvent('onclick');
        } else if (typeof DownloadLink.onclick == 'function') {
            DownloadLink.onclick();
        }

        document.body.removeChild(DownloadLink);
    }
};

export const downloadBlob = function(filename, content) {
    const blob = new Blob([content]);
    if ('download' in document.createElement('a')) { //支持a标签download的浏览器
        const link = document.createElement('a') // 创建a标签
        link.download = filename // a 标签添加属性
        link.style.display = 'none'
        link.href = URL.createObjectURL(blob)
        document.body.appendChild(link)
        link.click() // 执行下载
        URL.revokeObjectURL(link.href) // 释放url
        document.body.removeChild(link)// 释放标签
      } else { //其他浏览器
        navigator.msSaveBlob(blob, filename)
      }
}