preview-component.vue 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <template>
  2. <section>
  3. <!-- 图片预览 -->
  4. <el-dialog title="图片预览" :append-to-body="true" :visible.sync="previewVisible" width="50%">
  5. <div v-viewer>
  6. <img :src="previewPath" :alt="previewName" style="width:100%;height:100%" />
  7. </div>
  8. </el-dialog>
  9. </section>
  10. </template>
  11. <script>
  12. import { downloadUrl } from '@/api/file'
  13. import { getFileExt } from '@/api/util'
  14. import { component as Viewer } from 'v-viewer';
  15. export default {
  16. name: 'preview-component',
  17. components: { Viewer },
  18. data () {
  19. return {
  20. previewPath: '',
  21. previewName: '',
  22. previewVisible: false
  23. }
  24. },
  25. methods: {
  26. init (fileName, url) {
  27. this.handlePreview(fileName, url)
  28. },
  29. // 预览
  30. handlePreview (fileName, url) {
  31. if (!fileName || !url) {
  32. this.$message.error('没有可预览的文件!')
  33. return
  34. }
  35. let ext = getFileExt(fileName)
  36. if (ext === 'jpg' || ext === 'jpeg' || ext === 'png' || ext === 'gif') {
  37. this.previewPath = downloadUrl + url
  38. this.previewName = fileName
  39. this.previewVisible = false
  40. this.$viewerApi({
  41. images: [this.previewPath],
  42. options: { toolbar: false } // 隐藏工具栏
  43. });
  44. } else {
  45. // 弹出新页面显示下载文件
  46. window.open(downloadUrl + url, '_blank')
  47. }
  48. }
  49. }
  50. }
  51. </script>
  52. <style scoped>
  53. </style>