当前位置: 首页 > news >正文

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

需求

展示图片和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)$'; // 用于验证后缀是否是pdfvar 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><!-- 图片预览 --><viewerv-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)$'; // 用于验证后缀是否是pdfvar 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-undefPDFObject.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>

相关文章:

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

需求 展示图片和PDF类型&#xff0c;并且点击图片或者PDF可以预览 第一步&#xff1a;遍历所有的图片和PDF列表 <div v-for"(data,index) in parerFont(item.fileInfo)" :key"index" class"data-list-item"><downloadCard :file-inf…...

图论第5天

127.单词接龙 需要cout看一下过程。 #include <iostream> #include <queue> #include <stack> #include <unordered_map> #include <unordered_set> #include <vector> using namespace ::std;class Solution { public:int ladderLength(…...

Java开发-面试题-0004-HashMap 和 Hashtable的区别

Java开发-面试题-0004-HashMap 和 Hashtable的区别 更多内容欢迎关注我&#xff08;持续更新中&#xff0c;欢迎Star✨&#xff09; Github&#xff1a;CodeZeng1998/Java-Developer-Work-Note 技术公众号&#xff1a;CodeZeng1998&#xff08;纯纯技术文&#xff09; 生活…...

Swift 序列(Sequence)排序面面俱到 - 从过去到现在(一)

概览 在任何语言中对序列(或集合)元素的排序无疑是一种司空见惯的常规操作,在 Swift 语言里自然也不例外。序列排序看似简单,实则“暗藏玄机”。 要想真正掌握 Swift 语言中对排序的“各种姿势”,我们还得从长计议。不如就先从最简单的排序基本功开始聊起吧。 在本篇博…...

redis 04 redis结构

1.客户端...

【原创】springboot+mysql农业园区管理系统设计与实现

个人主页&#xff1a;程序猿小小杨 个人简介&#xff1a;从事开发多年&#xff0c;Java、Php、Python、前端开发均有涉猎 博客内容&#xff1a;Java项目实战、项目演示、技术分享 文末有作者名片&#xff0c;希望和大家一起共同进步&#xff0c;你只管努力&#xff0c;剩下的交…...

web前端 孙俏:深度探索与实战之路

web前端 孙俏&#xff1a;深度探索与实战之路 在这个数字化、信息化的时代&#xff0c;web前端技术以其独特的魅力&#xff0c;吸引着越来越多的开发者投身其中。今天&#xff0c;我们将跟随孙俏的脚步&#xff0c;一同探索web前端的深度与广度&#xff0c;揭开其神秘的面纱。…...

opencv实战小结-银行卡号识别

实战1-银行卡号识别 项目来源&#xff1a;opencv入门 项目目的&#xff1a;识别传入的银行卡照片中的卡号 难点&#xff1a;银行卡上会有一些干扰项&#xff0c;如何排除这些干扰项&#xff0c;并且打印正确的号码是一个问题 最终效果如上图 实现这样的功能需要以下几个步骤…...

Windows API 开发桌面应用程序,在窗口按下鼠标左键不放可以拖图,并且拖图期间鼠标图标变成手掌

在Windows API中&#xff0c;要实现鼠标左键按下并拖动以移动窗口中的某个图形&#xff0c;并且同时改变鼠标图标为“手掌”形状&#xff08;这通常指的是“拖动”或“移动”的图标&#xff09;&#xff0c;你需要执行几个步骤。 以下是一个基本的步骤指南&#xff0c;用于在W…...

Docker的网络管理

文章目录 一、Docker容器之间的通信1、直接互联&#xff08;默认Bridge网络&#xff09;1.1、Docker安装后默认的网络配置1.2、创建容器后的网络配置1.2.1、首先创建一个容器1.2.2、ip a 列出网卡变化信息1.2.3、查看新建容器后的桥接状态 1.3、容器内安装常见的工具1.4、容器间…...

【数据结构】平衡二叉树左旋右旋与红黑树

平衡二叉树左旋右旋与红黑树 平衡二叉树 定义 平衡二叉树是二叉搜索树的一种特殊形式。二叉搜索树&#xff08;Binary Search Tree&#xff0c;BST&#xff09;是一种具有以下性质的二叉树&#xff1a; 对于树中的每个节点&#xff0c;其左子树中的所有节点都小于该节点的值…...

2024蓝桥杯初赛决赛pwn题全解

蓝桥杯初赛决赛pwn题解 初赛第一题第二题 决赛getting_startedbabyheap 初赛 第一题 有system函数&#xff0c;并且能在bss上读入字符 而且存在栈溢出&#xff0c;只要过掉check函数即可 check函数中&#xff0c;主要是对system常规获取权限的参数&#xff0c;进行了过滤&…...

大模型多轮问答的两种方式

前言 大模型的多轮问答难点就是在于如何精确识别用户最新的提问的真实意图&#xff0c;而在常见的使用大模型进行多轮对话方式中&#xff0c;我接触到的只有两种方式&#xff1a; 一种是简单地直接使用 user 和 assistant 两个角色将一问一答的会话内容喂给大模型&#xff0c…...

【无标题】1877A

足球锦标赛中有 n支球队。每对队伍匹配一次。每场比赛结束后&#xff0c;Pak Chanek收到两个整数作为比赛结果&#xff0c;即两队在比赛中得分的数量。一支球队的效率等于本队每场比赛的总进球数减去对手每场比赛的总进球数。 比赛结束后&#xff0c;Pak Dengklek会计算每支球…...

直播美颜工具解析:美颜SDK核心技术与性能优化方法

本篇文章&#xff0c;小编将深入解析直播美颜SDK的核心技术及其性能优化方法&#xff0c;以期为开发者提供有价值的参考。 一、美颜SDK核心技术 1.实时人脸检测与识别 美颜SDK的核心技术之一是实时人脸检测与识别。这项技术基于深度学习算法&#xff0c;能够快速、准确地识别…...

YOLOv10开源,高效轻量实时端到端目标检测新标准,速度提升46%

前言 实时目标检测在自动驾驶、机器人导航、物体追踪等领域应用广泛&#xff0c;近年来&#xff0c;YOLO 系列模型凭借其高效的性能和实时性&#xff0c;成为了该领域的主流方法。但传统的 YOLO 模型通常采用非极大值抑制 (NMS) 进行后处理&#xff0c;这会增加推理延迟&#…...

如何解决访问网站时IP被限制的问题?

在互联网上&#xff0c;用户可能会面临一个令人困扰的问题——当尝试访问某个特定的网站时&#xff0c;却发现自己的IP地址被该网站屏蔽。 IP地址被网站屏蔽是一个相对常见的现象&#xff0c;而导致这种情况的原因多种多样&#xff0c;包括恶意行为、违规访问等。本文将解释IP地…...

springboot城市美发管理系统的设计与实现-计算机毕业设计源码71715

摘 要 信息化社会内需要与之针对性的信息获取途径&#xff0c;但是途径的扩展基本上为人们所努力的方向&#xff0c;由于站在的角度存在偏差&#xff0c;人们经常能够获得不同类型信息&#xff0c;这也是技术最为难以攻克的课题。针对城市美发管理系统等问题&#xff0c;对城市…...

微软 Windows 10 22H2 发布可选更新 19045.4474,修复窗口显示问题等

微软今天面向 Windows 10 22H2 版本&#xff0c;发布了 KB5037849 非安全可选更新&#xff0c;用户安装后版本号升至 Build 19045.4474。 IT之家 5 月 30 日消息&#xff0c;微软今天面向 Windows 10 22H2 版本&#xff0c;发布了 KB5037849 非安全可选更新&#xff0c;用户安…...

代码随想录算法训练营第五十三天 | 309.最佳买卖股票时机含冷冻期、714.买卖股票的最佳时机含手续费

309.最佳买卖股票时机含冷冻期 视频讲解&#xff1a;动态规划来决定最佳时机&#xff0c;这次有冷冻期&#xff01;| LeetCode&#xff1a;309.买卖股票的最佳时机含冷冻期_哔哩哔哩_bilibili代码随想录 解题思路 1. dp[i][0] 第i天持有股票的状态 dp[i][1]第i天不持股的状…...

业务系统对接大模型的基础方案:架构设计与关键步骤

业务系统对接大模型&#xff1a;架构设计与关键步骤 在当今数字化转型的浪潮中&#xff0c;大语言模型&#xff08;LLM&#xff09;已成为企业提升业务效率和创新能力的关键技术之一。将大模型集成到业务系统中&#xff0c;不仅可以优化用户体验&#xff0c;还能为业务决策提供…...

在鸿蒙HarmonyOS 5中实现抖音风格的点赞功能

下面我将详细介绍如何使用HarmonyOS SDK在HarmonyOS 5中实现类似抖音的点赞功能&#xff0c;包括动画效果、数据同步和交互优化。 1. 基础点赞功能实现 1.1 创建数据模型 // VideoModel.ets export class VideoModel {id: string "";title: string ""…...

Java如何权衡是使用无序的数组还是有序的数组

在 Java 中,选择有序数组还是无序数组取决于具体场景的性能需求与操作特点。以下是关键权衡因素及决策指南: ⚖️ 核心权衡维度 维度有序数组无序数组查询性能二分查找 O(log n) ✅线性扫描 O(n) ❌插入/删除需移位维护顺序 O(n) ❌直接操作尾部 O(1) ✅内存开销与无序数组相…...

理解 MCP 工作流:使用 Ollama 和 LangChain 构建本地 MCP 客户端

&#x1f31f; 什么是 MCP&#xff1f; 模型控制协议 (MCP) 是一种创新的协议&#xff0c;旨在无缝连接 AI 模型与应用程序。 MCP 是一个开源协议&#xff0c;它标准化了我们的 LLM 应用程序连接所需工具和数据源并与之协作的方式。 可以把它想象成你的 AI 模型 和想要使用它…...

拉力测试cuda pytorch 把 4070显卡拉满

import torch import timedef stress_test_gpu(matrix_size16384, duration300):"""对GPU进行压力测试&#xff0c;通过持续的矩阵乘法来最大化GPU利用率参数:matrix_size: 矩阵维度大小&#xff0c;增大可提高计算复杂度duration: 测试持续时间&#xff08;秒&…...

06 Deep learning神经网络编程基础 激活函数 --吴恩达

深度学习激活函数详解 一、核心作用 引入非线性:使神经网络可学习复杂模式控制输出范围:如Sigmoid将输出限制在(0,1)梯度传递:影响反向传播的稳定性二、常见类型及数学表达 Sigmoid σ ( x ) = 1 1 +...

Android Bitmap治理全解析:从加载优化到泄漏防控的全生命周期管理

引言 Bitmap&#xff08;位图&#xff09;是Android应用内存占用的“头号杀手”。一张1080P&#xff08;1920x1080&#xff09;的图片以ARGB_8888格式加载时&#xff0c;内存占用高达8MB&#xff08;192010804字节&#xff09;。据统计&#xff0c;超过60%的应用OOM崩溃与Bitm…...

初学 pytest 记录

安装 pip install pytest用例可以是函数也可以是类中的方法 def test_func():print()class TestAdd: # def __init__(self): 在 pytest 中不可以使用__init__方法 # self.cc 12345 pytest.mark.api def test_str(self):res add(1, 2)assert res 12def test_int(self):r…...

CSS设置元素的宽度根据其内容自动调整

width: fit-content 是 CSS 中的一个属性值&#xff0c;用于设置元素的宽度根据其内容自动调整&#xff0c;确保宽度刚好容纳内容而不会超出。 效果对比 默认情况&#xff08;width: auto&#xff09;&#xff1a; 块级元素&#xff08;如 <div>&#xff09;会占满父容器…...

LINUX 69 FTP 客服管理系统 man 5 /etc/vsftpd/vsftpd.conf

FTP 客服管理系统 实现kefu123登录&#xff0c;不允许匿名访问&#xff0c;kefu只能访问/data/kefu目录&#xff0c;不能查看其他目录 创建账号密码 useradd kefu echo 123|passwd -stdin kefu [rootcode caozx26420]# echo 123|passwd --stdin kefu 更改用户 kefu 的密码…...