navigator.msSaveOrOpenBlob,兼容ie11前端不能导出json文件

416 阅读1分钟

直接上代码


 // data为要保存导出的数据,fileName为要保存导出的文件名称
 saveJSON(data, fileName){
    let fileData = JSON.stringify(data, null, 4)
    let blob = new Blob([fileData])

    if(window.navigator.msSaveOrOpenBlob) {
        // 兼容ie11不能导出json
        window.navigator.msSaveOrOpenBlob(blob, fileName);
    }else{
        let aLink = document.createElement('a');
        aLink.href = URL.createObjectURL(blob);
        aLink.download = fileName;
        aLink.click();
        URL.revokeObjectURL(blob);
        aLink = null;
    }
}