Tool Function

Download

const exportAllRequest = () => {

    setAjaxLoading(true);

    get('url', null, {

        responseType: 'blob',

        headers: {

            "search-fields": encodeURIComponent(JSON.stringify(filterValue)),

            "sort-fields": encodeURIComponent(JSON.stringify(sort)),

            "type": BASECONSTANT.REQUEST.TYPE.REQUESTER,

            "is-rule": '1'

        }

    })

        .then(res => {

            setAjaxLoading(false);

            if (_.get(res, 'status') === 200) {

                download(res);

            } else {

                const msg = _.get(res, 'data.meta.info');

                showAlertInfo(msg, 'warning');

            }

        })

        .catch(err => {

            setAjaxLoading(false);

            setAjaxLoading(false);

            showAlertInfo('Request failed!', 'error');

        });

};

const download = (res) => {

    let blob = new Blob([res.data], { type: "application/vnd.ms-excel" });

    const blobURL = window.URL.createObjectURL(blob)

    const tempLink = document.createElement('a')

    tempLink.style.display = 'none'

    tempLink.href = blobURL

    tempLink.setAttribute('download', decodeURI(res.headers['content-disposition'].split(';')[1].split('=')[1]))

    if (typeof tempLink.download === 'undefined') {

        tempLink.setAttribute('target', '_blank')

    }

    document.body.appendChild(tempLink)

    tempLink.click()

    document.body.removeChild(tempLink)

    window.URL.revokeObjectURL(blobURL)

};

 

posted @ 2026-02-27 10:38  Summer_ee  阅读(2)  评论(0)    收藏  举报