{"size": 2374, "ext": "js", "max_stars_repo_path": "src/components/download.js", "max_stars_repo_name": "rejiejay/ycpd-IntelligentSurveillance", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/components/download.js", "max_issues_repo_name": "rejiejay/ycpd-IntelligentSurveillance", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/components/download.js", "max_forks_repo_name": "rejiejay/ycpd-IntelligentSurveillance", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 30.0506329114, "max_line_length": 119, "alphanum_fraction": 0.5935130581}
import myLoading from './myLoading';
import axios from 'axios';
import config from '@/config';

/**
 * 使用 get 请求下载
 */
export function downloadUsingGET(url) {
    myLoading.show(); // 每个请求出去的时候, 显示加载框（主要用来防止UI重复点击提交）

    axios({
        url: `${config.url.origin}${url}`,
        method: 'get',
        headers: {
            token: window.sessionStorage.cdimmstoken,
        },
        responseType: 'arraybuffer'
    }).then(response => {
        myLoading.close(); // 关闭加载框

        let contentdisposition = response.headers['content-disposition']; // "attachment; filename=LossAssessment.xlsx"
        let myFlieName = contentdisposition.slice(contentdisposition.indexOf('=') + 1);
        
        let blob = new Blob([response.data], {type: 'application/vnd.ms-excel;charset=utf-8'});
        let url = window.URL.createObjectURL(blob);
        let link = document.createElement('a');
        link.style.display = 'none';
        link.href = url;
        link.setAttribute('download', myFlieName);

        document.body.appendChild(link);
        link.click();

    }).catch(error => {
        myLoading.close(); // 关闭加载框
        
        console.error(error);
        alert(`导出失败, 原因: ${error.message}`);
    });
}

/**
 * 使用 post 请求下载
 */
export function downloadUsingPOST(url, data) {
    myLoading.show(); // 每个请求出去的时候, 显示加载框（主要用来防止UI重复点击提交）

    axios({
        url: `${config.url.origin}${url}`,
        method: 'post',
        headers: {
            token: window.sessionStorage.cdimmstoken,
        },
        data: data,
        responseType: 'arraybuffer'
    }).then(response => {
        myLoading.close(); // 关闭加载框

        let contentdisposition = response.headers['content-disposition']; // "attachment; filename=LossAssessment.xlsx"
        let myFlieName = contentdisposition.slice(contentdisposition.indexOf('=') + 1);
        
        let blob = new Blob([response.data], {type: 'application/vnd.ms-excel;charset=utf-8'});
        let url = window.URL.createObjectURL(blob);
        let link = document.createElement('a');
        link.style.display = 'none';
        link.href = url;
        link.setAttribute('download', myFlieName);

        document.body.appendChild(link);
        link.click();

    }).catch(error => {
        myLoading.close(); // 关闭加载框

        console.error(error);
        alert(`导出失败, 原因: ${error.message}`);
    });
}
