有关ant-designe-vue实现文件在线预览的代码

<template>
  <div>
    <a-upload
      :before-upload="beforeUpload"
      :on-success="onSuccess"
      multiple
      :show-upload-list="false"
    >
      <a-button>
        <a-icon type="upload" /> 选择文件
      </a-button>
    </a-upload>
    <ul class="file-list">
      <li
        v-for="(file, index) in fileList"
        :key="file.uid"
        :class="{ active: activeIndex === index }"
        @click="previewFile(index)"
      >
        <a-icon :type="file.icon" />
        {{ file.name }}
      </li>
    </ul>
    <a-modal
      title="文件预览"
      :visible="previewVisible"
      @cancel="handleCancel"
      :footer="null"
      width="800px"
    >
      <template v-if="previewFileType === 'image'">
        <img :src="previewUrl" alt="预览图片" style="max-width: 100%" />
      </template>
      <template v-else-if="previewFileType === 'pdf'">
        <iframe
          :src="previewUrl + '#zoom=100'"
          frameborder="0"
          style="width: 100%; height: 500px;"
        />
      </template>
      <template v-else-if="previewFileType === 'video'">
        <video :src="previewUrl" controls style="max-width: 100%" />
      </template>
      <template v-else-if="previewFileType === 'audio'">
        <audio :src="previewUrl" controls style="max-width: 100%" />
      </template>
      <template v-else-if="previewFileType === 'text'">
        <pre>{{ previewText }}</pre>
      </template>
      <template v-else>
        <p>不支持该类型文件的预览!</p>
      </template>
    </a-modal>
  </div>
</template>

<script>
import { Upload, Button, Icon, Modal } from 'ant-design-vue';

export default {
  components: {
    'a-upload': Upload,
    'a-button': Button,
    'a-icon': Icon,
    'a-modal': Modal,
  },
  data() {
    return {
      fileList: [],
      activeIndex: -1,
      previewVisible: false,
      previewUrl: '',
      previewFileType: '',
      previewText: '',
    };
  },
  methods: {
    beforeUpload(file) {
      const fileType = file.name.substring(file.name.lastIndexOf('.') + 1).toLowerCase();
      let icon = '';
      if (fileType === 'jpg' || fileType === 'png' || fileType === 'gif') {
        icon = 'picture';
      } else if (fileType === 'pdf') {
        icon = 'file-pdf';
      } else if (fileType === 'mp4' || fileType === 'ogg' || fileType === 'webm') {
        icon = 'video-camera';
      } else if (fileType === 'wav' || fileType === 'mp3') {
        icon = 'customer-service';
      } else if (fileType === 'txt' || fileType === 'html' || fileType === 'css' || fileType === 'js' || fileType === 'json') {
        icon = 'file-text';
      } else {
        return false;
      }
      this.fileList.push({
        uid: file.uid,
        name: file.name,
        icon,
        type: fileType,
        url: URL.createObjectURL(file),
      });
      return false;
    },
    onSuccess() {
      // 上传成功后的回调
    },
    previewFile(index) {
      this.activeIndex = index;
      const file = this.fileList[index];
      this.previewFileType = '';
      this.previewUrl = '';
      this.previewText = '';
      if (file.type === 'jpg' || file.type === 'png' || file.type === 'gif') {
        this.previewFileType = 'image';
        this.previewUrl = file.url;
      } else if (file.type === 'pdf') {
        this.previewFileType = 'pdf';
        this.previewUrl = file.url;
      } else if (file.type === 'mp4' || file.type === 'ogg' || file.type === 'webm') {
        this.previewFileType = 'video';
        this.previewUrl = file.url;
      } else if (file.type === 'wav' || file.type === 'mp3') {
        this.previewFileType = 'audio';
        this.previewUrl = file.url;
      } else if (file.type === 'txt' || file.type === 'html' || file.type === 'css' || file.type === 'js' || file.type === 'json') {
        this.previewFileType = 'text';
        const xhr = new XMLHttpRequest();
        xhr.open('GET', file.url);
        xhr.onreadystatechange = () => {
          if (xhr.readyState === 4) {
            if (xhr.status === 200) {
              this.previewText = xhr.responseText;
            } else {
              this.previewText = '文件读取失败!';
            }
          }
        };
        xhr.send();
      }
      this.previewVisible = true;
    },
    handleCancel() {
      this.previewVisible = false;
      this.activeIndex = -1;
      this.previewUrl = '';
      this.previewFileType = '';
      this.previewText = '';
    },
  },
};
</script>

<style scoped>
.file-list {
  margin-top: 20px;
  padding-left: 0;
  list-style: none;
}

.file-list li {
  display: inline-block;
  margin-right: 10px;
  cursor: pointer;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 5px;
}

.file-list li.active {
  background-color: #00a0e9;
  color: #fff;
}

.file-list li a-icon {
  font-size: 20px;
  margin-right: 5px;
}

.file-list li span {
  vertical-align: middle;
}

pre {
  white-space: pre-wrap;
  word-wrap: break-word;
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

还算善良_

如果对你的工作有所帮助,拜托啦

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

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

打赏作者

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

抵扣说明:

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

余额充值