您好,登錄后才能下訂單哦!
本篇文章為大家展示了使用vue怎么實現一個視頻上傳功能,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
Vue具體輕量級框架、簡單易學、雙向數據綁定、組件化、數據和結構的分離、虛擬DOM、運行速度快等優勢,Vue中頁面使用的是局部刷新,不用每次跳轉頁面都要請求所有數據和dom,可以大大提升訪問速度和用戶體驗。
video.vue
<template> <div class="container"> <el-card> <div slot="header"> <div>課程:</div> <div>階段:</div> <div>課時:</div> </div> <el-form label-width="40px"> <el-form-item label="視頻"> <input ref="video-file" type="file" > </el-form-item> <el-form-item label="封面"> <input ref="image-file" type="file" /> </el-form-item> <el-form-item> <el-button type="primary" @click="authUpload" >開始上傳</el-button> <el-button>返回</el-button> </el-form-item> </el-form> </el-card> </div> </template> <script> /* eslint-disable */ import axios from 'axios' import { aliyunImagUploadAddressAdnAuth, aliyunVideoUploadAddressAdnAuth, transCodeVideo, getAliyunTransCodePercent } from '@/services/aliyun-oss' export default { data () { return { uploader: null, videoId: null, imageUrl: '', fileName: '' } }, created () { this.initUploader() }, methods: { authUpload () { const videoFile = this.$refs['video-file'].files[0] this.uploader.addFile(videoFile, null, null, null, '{"Vod":{}}') this.uploader.addFile(this.$refs['image-file'].files[0], null, null, null, '{"Vod":{}}') this.fileName = videoFile.name this.uploader.startUpload() }, initUploader () { this.uploader = new window.AliyunUpload.Vod({ // 阿里賬號ID,必須有值 ,值的來源https://help.aliyun.com/knowledge_detail/37196.html userId: 1618139964448548, // 上傳到點播的地域, 默認值為'cn-shanghai',//eu-central-1,ap-southeast-1 region: 'cn-shanghai', // 分片大小默認1M,不能小于100K partSize: 1048576, // 并行上傳分片個數,默認5 parallel: 5, // 網絡原因失敗時,重新上傳次數,默認為3 retryCount: 3, // 網絡原因失敗時,重新上傳間隔時間,默認為2秒 retryDuration: 2, // 開始上傳 onUploadstarted: async uploadInfo => { console.log('onUploadstarted', uploadInfo) let uploadAuthInfo = null if (uploadInfo.isImage) { const { data } = await aliyunImagUploadAddressAdnAuth() this.imageUrl = data.data.imageURL uploadAuthInfo = data.data } else { const { data } = await aliyunVideoUploadAddressAdnAuth({ fileName: uploadInfo.file.name }) this.videoId = data.data.videoId uploadAuthInfo = data.data } // console.log('uploadAuthInfo', uploadAuthInfo) this.uploader.setUploadAuthAndAddress( uploadInfo, uploadAuthInfo.uploadAuth, uploadAuthInfo.uploadAddress, uploadAuthInfo.videoId || uploadAuthInfo.imageId ) }, // 文件上傳成功 onUploadSucceed: function (uploadInfo) { console.log('onUploadSucceed', uploadInfo) }, // 文件上傳失敗 onUploadFailed: function (uploadInfo, code, message) { console.log('onUploadFailed') }, // 文件上傳進度,單位:字節 onUploadProgress: function (uploadInfo, totalSize, loadedPercent) { }, // 上傳憑證超時 onUploadTokenExpired: function (uploadInfo) { console.log('onUploadTokenExpired') }, // 全部文件上傳結束 onUploadEnd: async uploadInfo => { console.log(uploadInfo) console.log({ lessonId: this.$route.query.lessonId, fileId: this.videoId, coverImageUrl: this.imageUrl, fileName: this.fileName }) const { data } = await transCodeVideo({ lessonId: this.$route.query.lessonId, fileId: this.videoId, coverImageUrl: this.imageUrl, fileName: this.fileName }) console.log(data) setInterval(async () => { const { data } = await getAliyunTransCodePercent(this.$route.query.lessonId) console.log('轉碼進度', data) }, 3000) } }) } } } </script>
aliyun-oss.ts ,存放接口
/** * 阿里云上傳 */ import request from '@/utils/request' export const aliyunImagUploadAddressAdnAuth = () => { return request({ method: 'GET', url: '/boss/course/upload/aliyunImagUploadAddressAdnAuth.json' }) } export const aliyunVideoUploadAddressAdnAuth = (params: any) => { return request({ method: 'GET', url: '/boss/course/upload/aliyunVideoUploadAddressAdnAuth.json', params }) } export const transCodeVideo = (data: any) => { return request({ method: 'POST', url: '/boss/course/upload/aliyunTransCode.json', data }) } export const getAliyunTransCodePercent = (lessonId: string | number) => { return request({ method: 'GET', url: '/boss/course/upload/aliyunTransCodePercent.json', params: { lessonId } }) }
上述內容就是使用vue怎么實現一個視頻上傳功能,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。