关于javascript:Util-前端-下载导出-文件

38次阅读

共计 684 个字符,预计需要花费 2 分钟才能阅读完成。

export const exportFile = (url, fileName) => {function createObjectURL(object) {return window.URL ? window.URL.createObjectURL(object) : window.webkitURL.createObjectURL(object)
    }
    let xhr = new XMLHttpRequest();
    let formData = new FormData();
    xhr.open('get', url);
    xhr.setRequestHeader('Auth-User-Token', getCookie('token'));
    xhr.responseType = 'blob';
    xhr.onload = function (e) {if (this.status === 200) {
            let blob = this.response;
            let newFileName = `${fileName}`;
            if (window.navigator.msSaveOrOpenBlob) {navigator.msSaveBlob(blob, newFileName)
            } else {let a = document.createElement('a');
                let url = createObjectURL(blob);
                a.href = url;
                a.download = newFileName;
                document.body.append(a);
                a.click();
                window.URL.revokeObjectURL(url);
            }
        }
    };
    xhr.send(formData)
};

正文完
 0