【HarmonyOS 5.0.0 或以上】构建文件预览组件 FilePreview:支持图片/PDF/音频等文件预览与下载

🎯 目标

封装一个通用文件预览组件 FilePreview,适用于:

  • 文件上传后的预览(图片 / PDF / 音频 / 文本)
  • 支持文件名、大小、下载按钮、文件类型图标
  • 图片支持缩略图点击放大
  • PDF / 音频使用系统能力或第三方框架预览
  • 适配桌面与移动端预览需求,后续支持权限控制与分布式打开

🧱 展示结构示意

🖼 图片.jpg   [预览] [下载]
📄 文档.pdf   [打开]
🎵 音频.mp3   [播放]

🧰 组件实现:FilePreview.ets

@Component
export struct FilePreview {
  @Prop files: Array<{
    name: string
    url: string
    type: 'image' | 'pdf' | 'audio' | 'text' | 'other'
  }> = []

  @Prop onDownload: (file: { name: string; url: string }) => void = () => {}
  @Prop onOpen: (file: { name: string; url: string }) => void = () => {}

  build() {
    Column({ space: 16 }) {
      this.files.forEach(file => {
        Row()
          .padding(12)
          .backgroundColor('#f8f8f8')
          .borderRadius(8)
          .alignItems(VerticalAlign.Center)
          .justifyContent(FlexAlign.SpaceBetween) {
          Row({ space: 8 }).alignItems(VerticalAlign.Center) {
            Text(this.getIcon(file.type)).fontSize(18)
            Text(file.name).fontSize(14).fontColor('#333').maxLines(1).textOverflow({ overflow: TextOverflow.Ellipsis })
          }

          Row({ space: 12 }) {
            if (file.type === 'image') {
              Button('预览').type(ButtonType.Normal).fontSize(12).onClick(() => this.previewImage(file.url))
            } else if (file.type === 'pdf' || file.type === 'audio' || file.type === 'text') {
              Button('打开').type(ButtonType.Normal).fontSize(12).onClick(() => this.onOpen(file))
            }

            Button('下载').type(ButtonType.Normal).fontSize(12).onClick(() => this.onDownload(file))
          }
        }
      })
    }
  }

  private getIcon(type: string): string {
    return {
      image: '🖼',
      pdf: '📄',
      audio: '🎵',
      text: '📝'
    }[type] ?? '📁'
  }

  private previewImage(url: string) {
    router.pushUrl(`/preview/image?src=${encodeURIComponent(url)}`)
  }
}

📦 使用示例

@Entry
@Component
struct DemoFilePreview {
  build() {
    FilePreview({
      files: [
        { name: '头像.jpg', url: 'https://example.com/avatar.jpg', type: 'image' },
        { name: '课程资料.pdf', url: 'https://example.com/doc.pdf', type: 'pdf' },
        { name: '背景音乐.mp3', url: 'https://example.com/music.mp3', type: 'audio' }
      ],
      onDownload: file => {
        console.info(`下载文件:${file.name}`)
      },
      onOpen: file => {
        console.info(`打开文件:${file.name}`)
      }
    })
  }
}

✨ 可扩展能力建议

功能说明
文件类型自动识别根据文件名后缀自动判断类型
预览大图全屏 + 滑动图片支持点击放大 + 多图滑动预览
文件权限控制(禁看/限下载)针对不同用户角色控制是否展示“下载/预览”按钮
分布式设备打开文件支持接入 startRemoteAbility 将文件投送到指定设备查看

📘 下一篇预告

第46篇:【HarmonyOS 5.0.0 或以上】构建语音输入组件 VoiceInput:集成语音识别 / 实时转文字 / 语音指令响应

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端付豪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值