乐趣区

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

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)
};
退出移动版