图片和PDF展示预览、并支持下载

news2024/10/6 0:29:05

需求

展示图片和PDF类型,并且点击图片或者PDF可以预览

第一步:遍历所有的图片和PDF列表

<div v-for="(data,index) in parerFont(item.fileInfo)" :key="index" class="data-list-item">
       <downloadCard :file-info="data" />
</div>

第二步:编写downloadcard组件

<template>
  <div class="download-card">
    <!-- 图片与PDF格式 -->
    <file-preview v-if="isImageOrPDF(fileInfo.url)" :url="fileInfo.url" :name="fileInfo.name" />
    <!-- ZIP XSLS WAP 等格式 -->
    <div v-else class="other-type">
      <div><d2-icon-svg name="transaction" class="iconrefresh" /></div>
      <div class="file-name-other">{{ fileInfo.name }}</div>
    </div>
    <div @click="donwload(fileInfo.url, fileInfo.name)">
      <d2-icon name="import" class="iconimport" />
    </div>
  </div>
</template>

<script>
import axios from 'axios';
import FilePreview from '@/components/file-preview';
export default {
  name: 'downloadCard',
  components: {
    FilePreview
  },
  props: {
    fileInfo: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
    };
  },
  created() {
  },
  methods: {
    justPDF(str) {
      var strRegex = '(.pdf)$'; // 用于验证后缀是否是pdf
      var reg = new RegExp(strRegex);
      if (reg.test(str.toLowerCase())) {
        // console.log('是pdf')
        return true;
      } else {
        // console.log('不是pdf')
        return false;
      }
    },
    isImageOrPDF(filename) {
      // 获取文件的扩展名
      const extension = filename.split('.').pop().toLowerCase();
      // 常见的图片和 PDF 文件扩展名
      const imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'svg'];
      const pdfExtensions = ['pdf'];
      // 检查文件扩展名是否在常见图片或 PDF 扩展名列表中
      return imageExtensions.includes(extension) || pdfExtensions.includes(extension);
    },
    donwload(fileUrl, fileName = '') {
      axios({
        method: 'get',
        url: fileUrl,
        responseType: 'blob',
      }).then((response) => {
        const link = document.createElement('a');
        const blob = new Blob([response.data], { type: response.data.type });
        const urlRef = window.URL.createObjectURL(blob);
        link.href = urlRef;
        link.download = fileName;
        link.click();
        window.URL.revokeObjectURL(urlRef);
      }).catch(err => {
        console.log(err);
      });
    }
  }
};
</script>

<style scoped lang="scss">
.download-card{
  padding: 12px;
  border-radius: 12px;
  border: 1px dashed rgba(234, 234, 234, 1);
  box-sizing: border-box;
  background: rgba(255, 255, 255, 1);
  display: flex;
  align-items: center;
  width: 235px;
  justify-content: space-between;
  color: rgba(114, 120, 128, 1);
  .file-name{
    width: 136px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    position: relative;
  }
  .file-name-other{
    width: 160px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    position: relative;
    margin-left: 5px;
  }
  .type-img{
    display: flex;
  }
  .type-pdf{
    display: flex;
  }
  .iconrefresh, .iconimport{
    width: 24px !important;
    height: 24px !important;
  }
  .other-type{
    display: flex;
    align-items: center;
    cursor: default;
  }
}
</style>

 第三步:编写FilePreview实现图片和PDF预览组件

<template>
  <div class="preview-wrap">
    <!-- PDF和图片类型点击 -->
    <div v-if="justPDF(url)" class="type-pdf" @click="previewFile(url)">
      <div><d2-icon-svg name="transaction" class="iconrefresh" /></div>
      <div class="file-name-pdf">{{ name }}</div>
    </div>
    <div v-else class="type-img" @click="previewFile(url)">
      <div>
        <img :src="url" class="type-img-preview" alt="">
      </div>
      <div class="file-name">{{ name }}</div>
      <div class="hover-pick">
        <img src="../../../public/image/merchant/Magnifying_glass.png" class="type-img-innner" alt="">
      </div>
    </div>
    <!-- PDF预览 -->
    <template v-if="pdfContainerVisible">
      <div id="pdf-container" />
      <div class="pdf-close" @click="pdfContainerVisible = false">
        <i class="el-icon-close" />
      </div>
    </template>
    <!-- 图片预览 -->
    <viewer
      v-else
      :options="{'toolbar': false, 'navbar': false, 'title': false}"
      :images="[url]"
      @inited="imagePreviewInited">
      <template slot-scope="scope">
        <img v-for="(src, index) in scope.images" :src="src" :key="index" style="display:none;">
      </template>
    </viewer>
  </div>
</template>

<script>
import Viewer from 'v-viewer/src/component.vue';
import 'viewerjs/dist/viewer.css';
// import MagnifyingGlass from '../../../public/image/merchant/Magnifying_glass.png';

import local from './local';

const SCOPE_NAME = 'siPreview';

export default {
  name: SCOPE_NAME,
  components: {
    Viewer,
  },
  inheritAttrs: false,
  props: {
    url: {
      type: String,
      default: '',
    },
    name: {
      type: String,
      default: '',
    },
    isImageBase64: false,
  },
  data() {
    return {
      pdfContainerVisible: false,
    };
  },
  created() {
    if (!this.$i18n.getLocaleMessage('en')[SCOPE_NAME]) {
      this.$i18n.mergeLocaleMessage('en', local.en);
      this.$i18n.mergeLocaleMessage('cn', local.cn);
    }
  },
  methods: {
    justPDF(str) {
      var strRegex = '(.pdf)$'; // 用于验证后缀是否是pdf
      var reg = new RegExp(strRegex);
      if (reg.test(str.toLowerCase())) {
        // console.log('是pdf')
        return true;
      } else {
        // console.log('不是pdf')
        return false;
      }
    },
    // 文件预览
    previewFile(fileUrl) {
      const fileExtension = fileUrl.split('.').pop();
      const isPdf = fileExtension.toLowerCase().startsWith('pdf');
      const isImg = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'].find(suffix => fileExtension.toLowerCase().startsWith(suffix));
      if (isPdf) {
        this.pdfPreview(fileUrl);
      } else if (isImg || this.isImageBase64) {
        this.imagePreview();
      } else {
        window.open(fileUrl);
      }
    },
    // 图片预览
    imagePreview() {
      this.$viewer.show();
    },
    imagePreviewInited(viewer) {
      this.$viewer = viewer;
    },
    // PDG预览
    pdfPreview(fileUrl) {
      this.pdfContainerVisible = true;
      this.$nextTick(() => {
        let url = '';
        if (fileUrl.startsWith('http://')) {
          url = fileUrl.substring(5);
        } else if (fileUrl.startsWith('https://')) {
          url = fileUrl.substring(6);
        } else {
          url = fileUrl;
        }
        // eslint-disable-next-line no-undef
        PDFObject.embed(url, '#pdf-container', {
          width: '80%'
        });
      });
    }
  },
};
</script>

<style lang="scss" scoped>
  .preview-wrap {
    display: inline-block;
    width: 224px;
    // background: red;
    height: 30px;
    margin-right: 5px;
  }
  .preview-button {
    padding: 0;
  }
  #pdf-container {
    position: fixed;
    z-index: 10000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    background-color: rgba(0, 0, 0, 0.6);
  }
  .pdf-close {
    position: fixed;
    z-index: 10001;
    right: 0;
    top: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 60px;
    height: 60px;
    font-size: 40px;
    color: #fff;
    background-color: rgba(0, 0, 0, 0.4);
    cursor: pointer;
  }
  .file-name{
    width: 136px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    position: relative;
    margin-left: 5px;
  }
  .file-name-pdf{
    width: 156px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    position: relative;
    margin-left: 5px;
  }
  .hover-pick{
    position: absolute;
    top: 0;
    background: #000;
    margin-top: -6px;
    border-radius: 8px;
    width: 42px;
    height: 42px;
    border: none;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    opacity: 0.5;
    visibility: hidden;
  }
  .type-img{
    display: flex;
    height: 30px;
    align-items: center;
    position: relative;
    .type-img-preview{
      height: 40px;
      width:40px;
      object-fit: cover;
      margin-right: 5px;
      margin-top: 3px;
      border-radius: 8px;
      border: 1px solid #EAEAEA;
    }
    .type-img-innner{
      height: 15px;
      width:15px;
      z-index: 2;
    }
    &:hover{
      .hover-pick{
        visibility:visible;
      }
    }
  }
  .type-pdf{
    display: flex;
    height: 30px;
    align-items: center;
  }
  .iconrefresh{
    width: 24px !important;
    height: 24px !important;
  }
</style>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1792661.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

STM32HAL-最简单的长、短、多击按键框架

目录 概述 一、开发环境 二、STM32CubeMx配置 三、编码 四、运行结果 五、总结 概述 本文章使用最简单的写法实现长、短、多击按键框架&#xff0c;非常适合移植各类型单片机&#xff0c;特别是资源少的芯片上。接下来将在stm32单片机上实现&#xff0c;只需占用1个定时…

spring 启动顺序

BeanFactoryAware 可在Bean 中获取 BeanFactory 实例 ApplicationContextAware 可在Bean 中获取 ApplicationContext 实例 BeanNameAware 可以在Bean中得到它在IOC容器中的Bean的实例的名字。 ApplicationListener 可监听 ContextRefreshedEvent等。 CommandLineRunner 整…

MySQL学习——选项文件的使用

MySQL 的许多程序都可以从选项文件&#xff08;有时也被称为配置文件&#xff09;中读取启动选项。选项文件提供了一种方便的方式来指定常用的选项&#xff0c;这样你就不必每次运行程序时都在命令行上输入这些选项。 要确定一个程序是否读取选项文件&#xff0c;你可以使用 -…

【工具箱】嵌入式系统存储芯片——CS创世 SD NAND

大家都知道MCU是一种"麻雀"虽小&#xff0c;却"五脏俱全"的主控。它的应用领域非常广泛&#xff0c;小到手机手表&#xff0c;大到航空航天的设备上都会用到MCU.市面上目前几个主流厂商有意法半导体&#xff08;其中最经典的一款就是STM32系列&#xff09;…

SSL证书国产化对提升我国网络安全的重要性

在互联网时代&#xff0c;数据安全和隐私保护成为全球关注的焦点。SSL证书作为保障网络通信安全的关键技术&#xff0c;其重要性不言而喻。然而&#xff0c;长期以来&#xff0c;我国在SSL证书领域过度依赖国外产品和技术&#xff0c;不仅存在安全隐患&#xff0c;也制约了我国…

LNMP网络架构的搭建

操作准备&#xff1a;准备三台虚拟机 安装 MySQL 服务 &#xff08;1&#xff09;准备好mysql目录上传软件压缩包并解压 cd /opt mkdir mysql tar xf mysql-boost-5.7.44.tar.gz &#xff08;2&#xff09;安装mysql环境依赖包 yum -y install ncurses ncurses-devel bison…

计算机网络之曼彻斯特编码和差分曼彻斯特编码

目录 前言 曼彻斯特编码 定义 策略 思路 差分曼彻斯特编码 定义 策略 思路 结束语 前言 今天是坚持写博客的第十九天&#xff0c;很高兴自己又坚持了一天&#xff0c;今天想送给自己一句李白《行路难》当中的诗词&#xff0c;希望我自己和大家都可以铭记于心&#x…

27、matlab傅里叶变换:fft()函数

1、fft 快速傅里叶变换 语法 Y fft(X) 使用快速傅里叶变换 (FFT) 算法计算 X 的离散傅里叶变换 (DFT)。 Y fft(X,n) 返回 n 点 DFT。 Y fft(X,n,dim) 返回沿维度 dim 的傅里叶变换。例如&#xff0c;如果 X 是矩阵&#xff0c;则 fft(X,n,2) 返回每行的 n 点傅里叶变换含…

微软不再允许Windows 11通过1@1.com绕过登录 但还有其他办法可以继续用

微软不再允许 Windows 11 通过 11.com 和 nothankyou.com 绕过登录&#xff0c;但断网的情况下使用 OOBE\BYPASSNRO 命令仍然是有效的。如果你在安装或重置系统时仍然需要创建本地账户&#xff0c;请直接使用 OOBE 命令。 在 Windows 11 家庭版和专业版中用户必须保持设备联网…

尚硅谷2024新版3小时速通Docker教程

尚硅谷2024新版3小时速通Docker教程 百度网盘&#xff1a;https://pan.baidu.com/s/1SncgHbdJehvZspjcrrbLSw?pwd6c27

linux 系统监控脚本

1.对CPU的监控函数 function GetCpu(){cpu_numgrep -c "model name" /proc/cpuinfocpu_usertop -b -n 1 | grep Cpu | awk {print $2} | cut -f 1 -d "%"cpu_systemtop -b -n 1 | grep Cpu | awk {print $4} | cut -f 1 -d "%"cpu_idletop -b -…

Github 2024-06-06 Go开源项目日报 Top10

根据Github Trendings的统计,今日(2024-06-06统计)共有10个项目上榜。根据开发语言中项目的数量,汇总情况如下: 开发语言项目数量Go项目10Ollama: 本地大型语言模型设置与运行 创建周期:248 天开发语言:Go协议类型:MIT LicenseStar数量:42421 个Fork数量:2724 次关注人…

当边缘计算用在定位设备

什么是边缘计算&#xff1f; 边缘计算是个比较高大上的概念&#xff0c;在这里就不提众多官方与非官方的定义了&#xff0c;只说说自己的理解。 边缘计算就是在最靠近物理设备的使用现场&#xff0c;利用有限的硬件资源&#xff0c;完成设备层数据采集、协议转换、数据上传、…

Java | Leetcode Java题解之第120题三角形最小路径和

题目&#xff1a; 题解&#xff1a; class Solution {public int minimumTotal(List<List<Integer>> triangle) {int n triangle.size();int[] f new int[n];f[0] triangle.get(0).get(0);for (int i 1; i < n; i) {f[i] f[i - 1] triangle.get(i).get(i…

Chrome浏览器打开无痕模式的方法

快捷键&#xff1a;同时按住CtrlshiftN 如图&#xff0c;系统会新开一个无痕标签页&#xff0c;不需要了点右上角关闭就可以了。

探索计算机视觉:开启智能图像处理的新纪元

第一部分&#xff1a;计算机视觉概述与基本原理 计算机视觉&#xff0c;作为人工智能领域的重要分支&#xff0c;旨在让计算机具备处理和理解图像和视频数据的能力。随着深度学习技术的飞速发展&#xff0c;计算机视觉已经在许多实际应用场景中取得了显著的成果&#xff0c;如…

山东大学软件学院项目实训-创新实训-基于大模型的旅游平台(二十七)- 微服务(7)

11.1 : 同步调用的问题 11.2 异步通讯的优缺点 11.3 MQ MQ就是事件驱动架构中的Broker 安装MQ docker run \-e RABBITMQ_DEFAULT_USERxxxx \-e RABBITMQ_DEFAULT_PASSxxxxx \--name mq \--hostname mq1 \-p 15672:15672 \-p 5672:5672 \-d \rabbitmq:3-management 浏览器访问1…

RabbitMQ-直连交换机(direct)使用方法

RabbitMQ-默认读、写方式介绍 RabbitMQ-发布/订阅模式 目录 1、概述 2、直连交换机 3、多重绑定 4、具体代码实现 4.1 生产者部分 4.2 消费者部分 5、运行代码 6、总结 1、概述 直连交换机&#xff0c;可以实现类似路由的功能&#xff0c;消息从交换机发送到哪个队列…

详细分析Mysql中的SQL_MODE基本知识(附Demo讲解)

目录 前言1. 基本知识2. Demo讲解2.1 ONLY_FULL_GROUP_BY2.2 STRICT_TRANS_TABLES2.3 NO_ZERO_IN_DATE2.4 NO_ENGINE_SUBSTITUTION2.5 ANSI_QUOTES 前言 了解Mysql内部的机制有助于辅助开发以及形成整体的架构思维 对于基本的命令行以及优化推荐阅读&#xff1a; 数据库中增…

【C语言】详解函数(下)(庖丁解牛版)

文章目录 1. 前言2. 数组做函数形参3. 函数嵌套调用和链式访问3.1 嵌套调用3.2 链式访问 1. 前言 详解C语言函数(上)的链接&#xff1a;http://t.csdnimg.cn/EGsfe 经过对函数的初步了解之后,相信大家已经对C语言标准库里的函数已经有初步的认知了&#xff0c;并且还学会了如…