美文网首页
封装下载函数

封装下载函数

作者: 青春向来如此 | 来源:发表于2021-05-07 15:47 被阅读0次

    src/utils/download.js

    import axios from "axios";
    import { ElMessage } from "element-plus";
    import { baseURL1 as baseURL } from "./axios.js";
    export function download({ url, params = {} }) {
        axios.get(baseURL + url, { params }).then(res => {
            if (res.data.code === 0) {
                ElMessage.error({
                    message: res.data.data,
                    type: "error"
                });
            } else {
                let url = window.URL.createObjectURL(new Blob([res.data]));
                let link = document.createElement("a");
                link.href = url;
                link.setAttribute("download", res.headers.filename);
                link.click();
            }
        });
    }
    export function hrefDownload(url) {
        const a = document.createElement("a");
        a.setAttribute("href", url);
        a.click();
    }
    

    相关文章

      网友评论

          本文标题:封装下载函数

          本文链接:https://www.haomeiwen.com/subject/cqtedltx.html