Skip to content

FilePreview 文件预览

支持图片、视频、音频、PDF、文本等多种文件类型的预览组件。

基础用法

预览图片文件。

vue
<template>
  <ExButton @click="visible = true">预览图片</ExButton>
  <ExFilePreview
    v-model:visible="visible"
    file="https://example.com/image.jpg"
    type="image"
    :zoomable="true"
    :rotatable="true"
  />
</template>

<script setup>
import { ref } from 'vue'

const visible = ref(false)
</script>

预览视频

支持视频文件预览。

vue
<template>
  <ExButton @click="visible = true">预览视频</ExButton>
  <ExFilePreview
    v-model:visible="visible"
    file="https://example.com/video.mp4"
    type="video"
  />
</template>

预览 PDF

支持 PDF 文件预览。

vue
<template>
  <ExButton @click="visible = true">预览 PDF</ExButton>
  <ExFilePreview
    v-model:visible="visible"
    file="https://example.com/document.pdf"
    type="pdf"
  />
</template>

图片操作

图片预览支持缩放、旋转等操作。

vue
<template>
  <ExFilePreview
    v-model:visible="visible"
    file="https://example.com/image.jpg"
    type="image"
    :zoomable="true"
    :rotatable="true"
    :show-toolbar="true"
  />
</template>

多文件预览

支持预览多个文件,可以切换查看。

vue
<template>
  <ExFilePreview
    v-model:visible="visible"
    :files="imageList"
    type="image"
    :initial-index="0"
  />
</template>

<script setup>
import { ref } from 'vue'

const visible = ref(false)
const imageList = ref([
  'https://example.com/image1.jpg',
  'https://example.com/image2.jpg',
  'https://example.com/image3.jpg',
])
</script>

自定义工具栏

可以自定义预览工具栏的内容。

vue
<template>
  <ExFilePreview
    v-model:visible="visible"
    file="https://example.com/image.jpg"
    type="image"
  >
    <template #toolbar>
      <ExButton size="small" @click="handleDownload">下载</ExButton>
      <ExButton size="small" @click="handleShare">分享</ExButton>
    </template>
  </ExFilePreview>
</template>

API

Props

属性说明类型默认值
visible / v-model:visible是否显示预览booleanfalse
file单个文件URL或对象string | PreviewFile-
files多个文件列表(string | PreviewFile)[]-
type文件类型PreviewType-
initial-index初始显示的文件索引number0
zoomable是否可缩放(图片)booleantrue
rotatable是否可旋转(图片)booleantrue
show-toolbar是否显示工具栏booleantrue
mask-closable点击遮罩是否关闭booleantrue
keyboard是否支持键盘操作booleantrue

Events

事件名说明类型
update:visible显示状态改变时触发(visible: boolean) => void
close关闭预览时触发() => void
change切换文件时触发(index: number) => void
zoom缩放时触发(scale: number) => void
rotate旋转时触发(angle: number) => void

Slots

插槽名说明
toolbar自定义工具栏内容
footer自定义底部内容

Methods

方法名说明类型
prev上一个文件() => void
next下一个文件() => void
zoomIn放大() => void
zoomOut缩小() => void
rotateLeft逆时针旋转() => void
rotateRight顺时针旋转() => void
reset重置() => void

类型定义

typescript
type PreviewType = 'image' | 'video' | 'audio' | 'pdf' | 'text' | 'unknown'

interface PreviewFile {
  url: string
  name?: string
  type?: PreviewType
  size?: number
}

无障碍支持

  • 完整的键盘导航支持(方向键、ESC等)
  • ARIA 属性标注
  • 屏幕阅读器友好
  • 焦点管理

主题定制

css
:root {
  --ex-preview-bg: rgba(0, 0, 0, 0.9);
  --ex-preview-toolbar-bg: rgba(0, 0, 0, 0.7);
  --ex-preview-toolbar-color: var(--ex-color-text-primary);
}