本文主要是介绍Vue+ElementUI实现文件照片音频视频预览,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. 需求是点击预览按钮 根据文件名的后缀去实现预览
2. 具体实现代码及逻辑
1.预览弹框
<el-dialog:visible.sync="visibleFile"width="40%":close-on-click-modal="false"@close="cancelHandler":append-to-body="true"><audiostyle="width: 100%;height: 100px;padding: 30px;margin-top: 10px;"v-if="isVideo"controls:src="previewUrl"></audio><videostyle="padding: 20px; margin-top: 20px"v-if="isAudio"width="100%"height="600"controls:src="previewUrl"></video><iframev-if="isExcel":src="excelPreviewUrl"frameborder="0"width="100%"height="600"></iframe><divstyle="width: 100%;height: 600px;display: flex;justify-content: center;align-items: center;"v-if="isImage"><imgclass="previewImg":src="previewUrl"alt=""style="max-width: 100%; max-height: 700px"/></div>
</el-dialog>
2.data定义isAudio: false, //视频isVideo: false, //音频isImage: false, //照片isExcel: false, //文件excelPreviewUrl: "", //文件地址previewUrl: "", //视频、音频、照片、文件地址
3.methodscancelHandler() {this.visibleFile = false;},// 预览会传把这一行的文件名拿到 然后取到后缀名,根据后缀名进行判断previewHandle(val) {this.visibleFile = true;if (val.fileExtension == "mp3") {this.previewUrl = val.materialUrl;this.isVideo = true;this.isAudio = false;this.isExcel = false;this.isImage = false;} else if (val.fileExtension == "mp4") {this.previewUrl = val.materialUrl;this.isAudio = true;this.isVideo = false;this.isExcel = false;this.isImage = false;} else if (val.fileExtension == "xlsx" || val.fileExtension == "xls") {const encodedFileUrl = encodeURIComponent(val.materialUrl); // 对文件URL进行编码this.excelPreviewUrl = `https://view.officeapps.live.com/op/view.aspx? src=${encodedFileUrl}`;// 显示预览this.isAudio = false;this.isVideo = false;this.isExcel = true;this.isImage = false;} else {this.previewUrl = val.materialUrl;// 显示预览this.isAudio = false;this.isVideo = false;this.isExcel = false;this.isImage = true;}},
这篇关于Vue+ElementUI实现文件照片音频视频预览的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!