解析文件流,后导出pdf文件

<template>
    <div>
        <p class="exportbjd" @click="exportbjd" v-permission="'viewPrice'">导出报价单</p>
    </div>
</template>
<script>
import { mapGetters } from 'vuex';
import axios from 'axios';
export default {
    props: ['num'],
    computed: {
        ...mapGetters({
            usename: 'usename',
            token: 'token',
        }),
    },
    methods: {
        // 导出报价单
        async exportbjd() {
            if (this.num == 0) {
                this.$message.warning('请勾选需要询价的商品');
            } else {
                axios
                    .get(process.env.VUE_APP_BASE_API + `/order/orders/exportQuote`, { responseType: 'blob', headers: { Authorization: this.token } })
                    .then(response => {
                        const url = window.URL.createObjectURL(new Blob([response.data], { type: 'application/pdf' }));
                        const link = document.createElement('a');
                        link.href = url;

                        // 提取文件名
                        const contentDispositionHeader = response.headers['content-disposition'];
                        let fileName = '报价单';
                        if (contentDispositionHeader) {
                            const fileNameMatch = contentDispositionHeader.match(/filename="(.+)"/);
                            if (fileNameMatch.length >= 2) {
                                fileName = fileNameMatch[1];
                            }
                        }

                        // 设置下载文件的属性(名称和类型)
                        link.setAttribute('download', fileName);
                        document.body.appendChild(link);
                        link.click();

                        // 清理创建的临时链接
                        URL.revokeObjectURL(url);
                    })
                    .catch(error => {
                        console.error(error);
                        // 处理错误
                    });
            }
        },
    },
};
</script>
<style lang="scss" scoped>
.exportbjd {
    cursor: pointer;
    padding: 2px 10px;
    color: $commonColor;
    border: 1px solid $commonColor;
    border-radius: 4px;
    font-size: 18px !important;
    height: 35px;
    text-align: center;
    line-height: 32px;
    margin-right: 10px;
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值