Vue和element中关于PDF的下载

本文介绍了如何在前端表格中展示保单号并实现点击下载功能,同时展示了如何通过e-policy-file-download-mixin.js进行Base64字符串转换为PDF文件的过程。涉及的技术包括Vue组件、axios调用、Base64编码和File-Saver库的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 <el-table-column label="保单号" show-overflow-tooltip min-width="220">
              <template slot-scope="scope">
                <span>{{ scope.row.policyNoCI }}</span>
                <span v-if="scope.row.policyNoCI" :key="scope.row.policyNoCI" class="link-type" @click="download(scope.row.policyNoCI)">下载</span>
              </template>
            </el-table-column>
js文件的写法
 import { ePolicyFileDownloadMixin } from '@/views/tool/policy-download/mixin/e-policy-file-download-mixin'
export default {
    name: 'HistoryProposal',
    mixins: [ ePolicyFileDownloadMixin],
 methods: {
  download(VISACODE) {
        const params = {
          KEY: 'getEpolicyFile',
          VISACODE,
          CALL_SYSTEM: 'xsqt',
          TYPE: 'E',
          FILE_TYPE: 'PDF'
        }
        this.getEPolicyFile(params, `${params.VISACODE}.pdf`, 'application/pdf')
      },
}
 
}

创建e-policy-file-download-mixin.js文件

import { base64String2FileAndSave } from '@/utils'
 // 电子保单下载
    getEPolicyFile(params, fileName, fileType) {
      this.downloading = true
      const _params = { data: { root: { EDI: { ...params } } } }
      getepolicyfile(_params)
        .then(data => {
          const base64String = data['return.pdf']
          try {
            const content = window.atob(base64String)
            JSON.parse(content)
            this.$message.error('电子保单不存在')
          } catch (e) {
            base64String2FileAndSave(base64String, fileName, fileType)
          }
          this.downloading = false
        })
        .catch(e => {
          this.downloading = false
          this.$message.error(e.toString())
        })
    }

utils.js中的base64String2FileAndSave方法

import { saveAs } from 'file-saver'//插件
/**
 * base64转文件并保存
 * @param str
 * @param fileName
 * @param fileType
 */
export function base64String2FileAndSave(str, fileName, fileType) {
  const s = window.atob(str)
  const buf = new ArrayBuffer(s.length)
  const view = new Uint8Array(buf)
  for (let i = 0; i !== s.length; ++i) {
    view[i] = s.charCodeAt(i) & 0xFF
  }
  saveAs(new Blob([buf], { type: fileType }), fileName)
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小仙有礼了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值