admin 管理员组

文章数量: 1087649

/*href:下载的链接;downloadName:下载到本地的文件名*/

downloadFile = (href, downloadName) => {
    // const oa = document.createElement('a');
    // let e = document.createEvent("MouseEvents");
    // e.initEvent("click", false, false); //初始化事件对象
    // oa.href = href; //设置下载地址
    // oa.download = ''; //设置下载文件名
    // oa.target = "_blank";
    // oa.dispatchEvent(e); //给指定的元素,执行事件click事件
    let xhr = new XMLHttpRequest();
    xhr.open("get", href, true);
    xhr.responseType = "blob";
    xhr.onload = () => {
        if (this.status === 200) {
            const blob = new Blob([this.response], { type: this.response.type });
            let url = window.URL.createObjectURL(blob);
            const link = document.createElement('a');
            link.href = url;
            link.download = downloadName;
            link.click();
            URL.revokeObjectURL(url);
        }
    };
    xhr.send();
};

本文标签: 点击下载 浏览器 文件 图片 视频