当前位置:   article > 正文

Vue+ElementUI实现文件照片音频视频预览

Vue+ElementUI实现文件照片音频视频预览

1. 需求是点击预览按钮  根据文件名的后缀去实现预览

2. 具体实现代码及逻辑

  1. 1.预览弹框
  2. <el-dialog
  3. :visible.sync="visibleFile"
  4. width="40%"
  5. :close-on-click-modal="false"
  6. @close="cancelHandler"
  7. :append-to-body="true">
  8. <audio
  9. style="
  10. width: 100%;
  11. height: 100px;
  12. padding: 30px;
  13. margin-top: 10px;
  14. "
  15. v-if="isVideo"
  16. controls
  17. :src="previewUrl"
  18. ></audio>
  19. <video
  20. style="padding: 20px; margin-top: 20px"
  21. v-if="isAudio"
  22. width="100%"
  23. height="600"
  24. controls
  25. :src="previewUrl"
  26. ></video>
  27. <iframe
  28. v-if="isExcel"
  29. :src="excelPreviewUrl"
  30. frameborder="0"
  31. width="100%"
  32. height="600"
  33. >
  34. </iframe>
  35. <div
  36. style="
  37. width: 100%;
  38. height: 600px;
  39. display: flex;
  40. justify-content: center;
  41. align-items: center;
  42. "
  43. v-if="isImage"
  44. >
  45. <img
  46. class="previewImg"
  47. :src="previewUrl"
  48. alt=""
  49. style="max-width: 100%; max-height: 700px"
  50. />
  51. </div>
  52. </el-dialog>
  53. 2.data定义
  54. isAudio: false, //视频
  55. isVideo: false, //音频
  56. isImage: false, //照片
  57. isExcel: false, //文件
  58. excelPreviewUrl: "", //文件地址
  59. previewUrl: "", //视频、音频、照片、文件地址
  60. 3.methods
  61. cancelHandler() {
  62. this.visibleFile = false;
  63. },
  64. // 预览会传把这一行的文件名拿到 然后取到后缀名,根据后缀名进行判断
  65. previewHandle(val) {
  66. this.visibleFile = true;
  67. if (val.fileExtension == "mp3") {
  68. this.previewUrl = val.materialUrl;
  69. this.isVideo = true;
  70. this.isAudio = false;
  71. this.isExcel = false;
  72. this.isImage = false;
  73. } else if (val.fileExtension == "mp4") {
  74. this.previewUrl = val.materialUrl;
  75. this.isAudio = true;
  76. this.isVideo = false;
  77. this.isExcel = false;
  78. this.isImage = false;
  79. } else if (val.fileExtension == "xlsx" || val.fileExtension == "xls") {
  80. const encodedFileUrl = encodeURIComponent(val.materialUrl); // 对文件URL进行编码
  81. this.excelPreviewUrl = `https://view.officeapps.live.com/op/view.aspx?
  82. src=${encodedFileUrl}`;
  83. // 显示预览
  84. this.isAudio = false;
  85. this.isVideo = false;
  86. this.isExcel = true;
  87. this.isImage = false;
  88. } else {
  89. this.previewUrl = val.materialUrl;
  90. // 显示预览
  91. this.isAudio = false;
  92. this.isVideo = false;
  93. this.isExcel = false;
  94. this.isImage = true;
  95. }
  96. },

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/532426
推荐阅读
相关标签
  

闽ICP备14008679号