vue中使用文件流进行下载(new Blob),不打开一个新页面下载

export function download (url, params, filename) {

  Message.warning('导出数据中')

  return axios.get(url, {

    params: params,

    responseType:'arraybuffer',

  }).then((r) => {

    const content = r.data

    const blob = new Blob([content],{type:'application/vnd.ms-excel'})

    if ('download' in document.createElement('a')) {

      const elink = document.createElement('a')

      elink.download = filename

      elink.style.display = 'none'

      elink.href = URL.createObjectURL(blob)

      document.body.appendChild(elink)

      elink.click()

      URL.revokeObjectURL(elink.href)

      document.body.removeChild(elink)

      Message.success('导出成功')

    }



  }).catch((r) => {

    console.error(r)

    Message.error('导出失败')

  })

}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Vue,您可以使用第三方JavaScript库来批量下载PDF文件并将它们合并到一个PDF一个常用的库是`pdf-lib`,它可以通过JavaScript来创建、修改和合并PDF文件。 首先,您需要使用`pdf-lib`的API读取每个PDF文件并将其存储为`PDFDocument`对象。接下来,您可以使用这些对象的`copyPages`方法将所有页面复制到一个新的`PDFDocument`对象。最后,您可以使用`save`方法将新的`PDFDocument`保存为单个PDF文件并提供下载链接。 以下是一个示例代码片段,展示了如何使用`pdf-lib`在Vue批量下载和合并PDF文件: ```javascript import { PDFDocument } from 'pdf-lib' async function mergePDFs(urls) { const pdfDocs = [] for (const url of urls) { const existingPdfBytes = await fetch(url).then(res => res.arrayBuffer()) const pdfDoc = await PDFDocument.load(existingPdfBytes) pdfDocs.push(pdfDoc) } const mergedPdf = await PDFDocument.create() for (const pdfDoc of pdfDocs) { const copiedPages = await mergedPdf.copyPages(pdfDoc, pdfDoc.getPageIndices()) copiedPages.forEach((page) => mergedPdf.addPage(page)) } const mergedPdfFile = await mergedPdf.save() const downloadLink = document.createElement('a') downloadLink.href = URL.createObjectURL(new Blob([mergedPdfFile], { type: 'application/pdf' })) downloadLink.setAttribute('download', 'merged.pdf') document.body.appendChild(downloadLink) downloadLink.click() } ``` 上面的代码通过提供一个包含多个PDF文件的URL数组来合并这些文件,并在用户单击下载链接时提供下载。您可以将此函数添加到Vue组件并在需要时调用它,以在Vue应用程序批量下载和合并PDF文件
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值