vue pdf文件导出

vue pdf文件导出,后台返回文件流

const config = {
    method: 'get',
    url:‘’,  这里显示请求地址
    headers: {
        //和后端设置的一样
        'Content-Type': 'application/octet-stream;charset=UTF-8',
        'Authorization':‘’   这里需要token,可以加上
    },
    params: { id: id },    需要传递的参数
    responseType: 'blob'
};
axios(config).then(response => {
    const url = window.URL.createObjectURL(new Blob([response.data]));
    const link = document.createElement('a');
    link.href = url;
    const disposition = response.headers["content-disposition"];   后台返回的文件命名
    link.setAttribute('download', decodeURI(disposition.split("filename=")[1])+'.pdf');这获取文件名,前端需要转换
    document.body.appendChild(link);
    link.click();
    link.remove();
})

就是这么简单,soeasy


版权声明:本文为weixin_38655115原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。