Vue中使用Blob进行文件下载

一、 常规用法 chrome正常使用,IE11 、Firefox 不兼容
    let a = document.createElement('a');
    let blob = new Blob([response.data], {
     type: "application/vnd.ms-excel"}); 
    let objectUrl = URL.createObjectURL(blob);
    a.setAttribute("href",objectUrl);
    a.setAttribute("download", 'XXX.xls');
    a.click();
二、兼容 IE11、FireFox、Chrome
    var csvData = new Blob([content], {
      type: 'application/vnd.ms-excel' });
    // for IE
    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
     
        window.navigator.msSaveOrOpenBlob(csvData, file_name);
    }
    // for Non-IE (chrome, firefox etc.)
    else {
     
        var a = document.createElement('a');
        //FireFox 需要补充 document.body.appendChild(a) 语法提供支持
        document.body.appendChild(a);
        a.style = 'display: none';
        var url = window.URL.createObjectURL(csvData);
        a.href =  url;
        a.download = file_name;
        a.click();
        a.remove();
        window.URL.revokeObjectURL(url);
    }
三、参考链接

https://blog.csdn.net/u014628388/article/details/81738704

你可能感兴趣的:(前端框架,vue.js,javascript)