<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>
解析文件流,后导出pdf文件
最新推荐文章于 2024-09-28 19:02:04 发布