el-upload 上传.pdf文件,并读取文件内容

<template>
  <div>
    <el-upload class="upload-demo" :action="/file/upload/" accept=".pdf" name="upfile" :on-success="
                  (response, file, fileList) => {
                    return handleImgSuccess(response, file, fileList)
                  }
                ">
      <el-button size="small" type="primary">点击上传</el-button>
    </el-upload>
    <div v-html="pdfContent"></div>
  </div>
</template>

<script>
import pdfjsLib from "pdfjs-dist";
export default {
  data() {
    return {
      pdfContent: "",
    };
  },
  methods: {
    handleImgSuccess(res, file) {
      console.log(res, file);
      const fileObj = file.raw;
      if (fileObj.type === "application/pdf") {
        const reader = new FileReader();
        reader.onload = (e) => {
          const arrayBuffer = e.target.result;
          this.parsePdfFile(arrayBuffer);
        };
        reader.readAsArrayBuffer(fileObj);
      } else {
        this.$message.error("只能上传.pdf文件");
      }
    },
    parsePdfFile(arrayBuffer) {
      pdfjsLib
        .getDocument(arrayBuffer)
        .promise.then((pdf) => {
          let pageNumber = 1;
          let pagePromises = [];
          while (pageNumber <= pdf.numPages) {
            pagePromises.push(this.getPageText(pdf, pageNumber));
            pageNumber++;
          }
          Promise.all(pagePromises)
            .then((pages) => {
              let text = "";
              pages.forEach((page) => {
                text += page;
              });
              this.pdfContent = text;
            })
            .catch((error) => {
              console.error("解析.pdf文件失败", error);
            });
        })
        .catch((error) => {
          console.error("读取文件失败", error);
        });
    },
    getPageText(pdf, pageNumber) {
      return new Promise((resolve, reject) => {
        pdf
          .getPage(pageNumber)
          .then((page) => {
            page.getTextContent().then((textContent) => {
              let text = "";
              textContent.items.forEach((item) => {
                text += item.str + " ";
              });
              resolve(text);
            });
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
  },
};
</script>

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用以下代码示例来实现通过点击el-upload组件上文件进行预览PDF和图像。 ``` <template> <el-upload class="upload-demo" action="/your-upload-api" :on-preview="handlePreview" :before-upload="beforeUpload" :file-list="fileList" :auto-upload="false" :multiple="true" list-type="picture-card"> <i class="el-icon-plus"></i> <div class="upload-text">点击上</div> </el-upload> </template> <script> export default { data() { return { fileList: [], }; }, methods: { beforeUpload(file) { const isJPG = file.type === 'image/jpeg'; const isPNG = file.type === 'image/png'; const isPDF = file.type === 'application/pdf'; const isLt2M = file.size / 1024 / 1024 < 2; if (!isJPG && !isPNG && !isPDF) { this.$message.error('只能上 JPG、PNG、PDF 格式的文件!'); return false; } if (!isLt2M) { this.$message.error('上文件大小不能超过 2MB!'); return false; } return true; }, handlePreview(file) { if (file.type === 'application/pdf') { // 预览 PDF 文件 window.open(file.url); } else { // 预览图像文件 this.$viewer.show(file.url, { title: file.name, navbar: true, toolbar: { zoomIn: 4, zoomOut: 4, oneToOne: 4, reset: 4, prev: 0, play: { show: false, }, next: 0, rotateLeft: 0, rotateRight: 0, flipHorizontal: 0, flipVertical: 0, }, }); } }, }, }; </script> ``` 在上面的示例中,我们使用了element-ui的el-upload组件,并设置了上文件的类型和大小限制。我们还绑定了before-upload和on-preview事件,处理上和预览文件的逻辑。 在before-upload方法中,我们检查上文件类型和大小是否符合要求,并返回true或false。如果返回false,则不会上文件。 在handlePreview方法中,我们根据文件类型分别处理预览PDF和图像文件的逻辑。对于PDF文件,我们使用window.open打开文件URL以进行预览。对于图像文件,我们使用$viewer插件打开文件URL以进行预览。您需要在项目中安装和导入此插件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值