惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
博客园_首页
S
SegmentFault 最新的问题
罗磊的独立博客
博客园 - 【当耐特】
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
D
Docker
雷峰网
雷峰网
Google DeepMind News
Google DeepMind News
博客园 - 司徒正美
V
V2EX
大猫的无限游戏
大猫的无限游戏
V
Visual Studio Blog
腾讯CDC
宝玉的分享
宝玉的分享
酷 壳 – CoolShell
酷 壳 – CoolShell
人人都是产品经理
人人都是产品经理
T
Tailwind CSS Blog
Vercel News
Vercel News
H
Help Net Security
博客园 - Franky
D
DataBreaches.Net
aimingoo的专栏
aimingoo的专栏

博客园 - IT之家

关于antd前端组件a-select组件选择无效的问题处理记录 swagger文档生成html静态文档 根据Word模板导出word文档,包含文本标签替换为图片,生成列表数据,以及标签替换等功能 asp.net core中webapi接口的动作与参数 .NETCore文件上传将文件保存到docker容器以外的文件夹 liunx服务器通过ssh实现异地备份 mysql常见错误 liunx下自动备份文件夹 mysql在liunx下面的自动备份 关于.net6.0中swagger偶尔无法加载接口的问题笔记 VUE对象数组,和普通数组的常用方法 关于VUE项目请示接口报错:Error: Network Error at createError (createError.js:17) at XMLHttpRequest.handleError的问题处理 解决.net6 Docker容器 DateTime.Now 获取时间相差8小时问题(转载) CronExpression使用笔记 vue项目发布到docker nginx的方法总结 VUE项目开发报错:Error in render: "TypeError: Cannot read property 'country' of undefined" vs2022升级后打开项目报错预定义类型system.void未定义或导入” 关于.net6项目发布到docker(nginx)踩到的一些坑 关于.netcore6图片处理(生成缩略图、验证码、加水印)发布到docker(liunx)踩到的坑
antd前端页面实现浏览器中PDF文件预览(不使用第三方插件 )
IT之家 · 2025-08-01 · via 博客园 - IT之家

默认情况下在浏览器里面输入pdf文件路径(网址)会下载,现在项目里面需要实现pd文件在浏览中预览和打印的功能,研究了半天终于实现了不使用插件使用浏览器自带的PDF预览功能进行PDF文件预览

以下代码是一个ant组件页面的代码,组件功能,实现不同的文件类型显示不同的文件图片,并且实现下载和预览的功能

其中文件的图标是在前面项目的静态图片

<template>
  <a-image class="cImg" v-if="isImage(itemdata.fileName)" :src="itemdata.url" />
  <img v-else-if="isDocument(itemdata.fileName)" src="@/assets/images/document.png" :alt="itemdata.fileName" />
  <img v-else-if="isRar(itemdata.fileName)" src="@/assets/images/rar.png" :alt="itemdata.fileName" />
  <img v-else-if="isMedia(itemdata.fileName)" src="@/assets/images/media.png" :alt="itemdata.fileName" />
  <img v-else src="@/assets/images/other.png" :alt="itemdata.fileName" />
  <a-button style="margin-top: 20px; margin-right: 10px" type="link" @click="download(itemdata)">下载</a-button>
  <a-button
    style="margin-top: 20px"
    v-if="isDocument(itemdata.fileName) || isPDF(itemdata.fileName)"
    type="primary"
    @click="previewFile(itemdata)"
    >预览</a-button
  >
</template>

<script setup name="attachview">
  import { defineProps, toRef, defineExpose, ref, nextTick } from 'vue'
  import axios from 'axios'
  const pageCount = ref(0)
  const props = defineProps({
    itemdata: {
      type: Object,
      default: () => {},
      required: true
    }
  })
  const imageExtensions = /\.(jpg|jpeg|png|gif|bmp|webp)$/i
  const documentExtensions = /\.(doc|docx|ppt|pptx|xls|xlsx)$/i
  const rarExtensions = /\.(rar|zip|7z|tar|iso|gzip)$/i
  const mediaExtensions = /\.(mp3|mp4|wmv|aiv|mov|flv|mkv)$/i
  const isImage = (filename) => {
    if (!filename) return false
    return imageExtensions.test(filename.toLowerCase())
  }
  const isDocument = (filename) => {
    if (!filename) return false
    return documentExtensions.test(filename.toLowerCase())
  }
  const isRar = (filename) => {
    if (!filename) return false
    return rarExtensions.test(filename.toLowerCase())
  }
  const isMedia = (filename) => {
    if (!filename) return false
    return mediaExtensions.test(filename.toLowerCase())
  }
  const previewFile = (pdfdata) => {
    axios
      .get(pdfdata.url, {
        responseType: 'arraybuffer',
        headers: {
          'Content-Disposition': 'inline;filename=' + encodeURIComponent(pdfdata.fileName)
        }
      })
      .then((response) => {
        console.log(response.status) // 输出状态码
        console.log(response.headers) // 输出响应头信息
        var contype = response.headers['content-type']
        const blob = new Blob([response.data], { type: contype }) // 文件的content-type在后端接口中返回
        const url = window.URL.createObjectURL(blob)  // 以预览模式在浏览器中打开文件
        window.open(url, pdfdata.fileName)
      })
  }
  const download = (data) => {
    window.open(data.url) // 打开文件的下载链接
  }
  // 类型判断
  const isPDF = (filename) => /\.pdf$/i.test(filename)
</script>

<style scoped></style>