vue+axios下載pdf文件流

項目中用到流文件下載的需求,以前使用的方法一直都沒問題,可是此次就是下載不下來,查了多種方法終於解決了,方式以下:javascript

// 下載文件
downLoadFile(e) {
    let id = e.target.dataset.id;
    let name = e.target.dataset.name;

    if(e.target.dataset.id) {
        this.$https({
            method: 'get',
            url: '/wyxt_ubqts_oncloud/common/download',
            params: {
                'file_id': '526f260d-73c1-422e-b10e-c0a2fe91364d'
            },
            responseType: "blob"
        }).then(res => {
            // console.log(res)
            // const fileName = res.headers["content-disposition"].split("=")[1];
            const _res = res.data;
            let blob = new Blob([_res], {type: 'application/pdf'});
            let downloadElement = document.createElement("a");
            let href = window.URL.createObjectURL(blob); //建立下載的連接
            downloadElement.href = href;
            // downloadElement.download = fileName; //下載後文件名
            downloadElement.download = name; //下載後文件名
            document.body.appendChild(downloadElement);
            downloadElement.click(); //點擊下載
            document.body.removeChild(downloadElement); //下載完成移除元素
            window.URL.revokeObjectURL(href); //釋放掉blob對象
        });
    }
},
相關文章
相關標籤/搜索