vue 超简单 oss分片上传文件 大文件上传阿里云

本次上传视频使用的uniapp开发,需要安装oss插件

只需要两步:第一步选择文件,第二部保存上传

1、先写界面,点击选择要上传的视频文件的按钮

代码如下:

javascript 复制代码
<input ref="fileInput" type="file" accept=".mp3,.wav,.mp4,.mov,.m4a,.aac,.ogg" style="display: none"
          @change="handleFileChange" />

handleFileChange方法内容如下:

javascript 复制代码
async handleFileChange(event) {
        const selectedFiles = event.target.files
        if (!selectedFiles || selectedFiles.length === 0) return
        const file = selectedFiles[0]
        // 重置input,允许再次选择相同文件
        event.target.value = null
        // 处理每个文件
        // 检查文件类型
        if (file.type.startsWith('audio/wav') && file.type.startsWith('audio/mp3') && file.type.startsWith(
            'video/mp4')) {
          this.$message.warning('不支持的文件类型:请上传音频或视频格式为WAV的文件。') // i18n: 不支持的文件类型: {type}。请上传音频或视频文件。
          return
        }
        // 检查文件大小 (100MB)
        // if (file.size > 100 * 1024 * 1024) {
        //   this.$message.warning('文件大小超过限制 (最大10MB)')
        //   return
        // }
        // 创建预览URL,声音试听 本地文件
        const previewUrl = URL.createObjectURL(file)
        // 当前选择的文件
        this.upLoadMediaFile = {
          name: file.name,
          type: file.type,
          size: file.size,
          file: file,
          previewUrl: previewUrl,
          duration: 0, // 总时长
          loading: true, // 显示加载状态
          currentTime: 0 // 当前播放的长度
        }

        // 获取媒体文件时长
        await this.getMediaDuration(file, this.upLoadMediaFile)


        // 隐藏上传页面 ,显示 试听 页面
        this.showUpLoad = false
        this.showCloneListen = true
      },

// 获取媒体文件时长
      async getMediaDuration(file, fileObj) {
        const that = this
        // 根据文件类型创建不同的媒体元素
        this.cloneMedia = file.type.startsWith('audio/') ?
          new Audio() :
          document.createElement('video')
        this.cloneMedia.src = URL.createObjectURL(file)
        // 设置事件监听器
        this.cloneMedia.addEventListener('loadedmetadata', () => {
          // 获取到时长后更新文件对象
          fileObj.duration = this.cloneMedia.duration
          that.cloneViceCloneDuration = parseInt(fileObj.duration)
          that.radioProDuration = that.formatToMinSec(that.cloneViceCloneDuration)
          // 判断文件时长
          if (this.cloneViceCloneDuration > 5 && this.audioType == "0") {
            this.$message.warning(this.$t('uploadAudio.basicVersionDurationLimit')) // 原文:基础版音频,要求控制音频时长在5秒以内
            this.upLoadMediaFile = null;
            return
          }
          if (this.cloneViceCloneDuration < 15 && this.audioType == "1") {
            this.$message.warning(this.$t(
              'uploadAudio.highFidelityDurationLimit')) // 原文:高保真音频,要求控制音频时长在15秒以上,请放慢语速
            this.upLoadMediaFile = null;
            return
          }
          that.orignlaDuration = this.cloneMedia.duration
          fileObj.loading = false
        })

        this.cloneMedia.addEventListener('error', (e) => {
          fileObj.loading = false
          this.cloneMedia = null
          URL.revokeObjectURL(this.cloneMedia.src)
        })
        // 媒体播放进度
        this.cloneMedia.addEventListener('timeupdate', (e) => {
          if (this.cloneMedia) {
            fileObj.currentTime = this.cloneMedia.currentTime
            that.radioProCurrent = that.formatToMinSec(fileObj.currentTime)
            // 这里可以添加您的自定义逻辑
            that.cloneViceCurrentTime = parseInt(fileObj.currentTime)
            if (that.orignlaDuration) {
              that.orgPercentage = parseInt(100 * this.cloneMedia.currentTime / that.orignlaDuration)
            }
          }
        })
        this.cloneMedia.addEventListener('ended', () => {
          this.pauseAudioStatus()
        })
        // 声音播放完成
        this.cloneMedia.addEventListener('pause', () => {
          this.pauseAudioStatus()
        })
        // 加载元数据
        this.cloneMedia.load()
      },
2、上传保存到oss

其中await getStsACS(params);根据自己的需求获取对应的阿里云临时平时

javascript 复制代码
async setAss() {
        if (!this.upLoadMediaFile) {
          this.$message.warning(this.$t('uploadAudio.pleaseUploadAudio')) // 原文:请上传音频
          return
        }
        const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        let that = this;
        // that.showLoading(this.$t('drawvoiceclone.submitting')) // i18n: 正在提交...
        let tmpFile = this.upLoadMediaFile;
        let tmpName = tmpFile.name + "";
        let lastName = tmpName.substring(tmpFile.name.indexOf("."), tmpName.length);
        let name = 's' + that.random_string(6) + '_' + new Date().getTime() + lastName;
        const params = {
          sourceType: "1.1",
          userId: that.userInfo.userId
        }
        let response = await getStsACS(params);
        if (response.code == 0) {
          let responseData = response.data
          //成功
          name = responseData.keyPrefix ? responseData.keyPrefix + name : name
          this.client = new OSS({
            region: responseData.region ? responseData.region : 'oss-cn-shanghai', // 替换为你的实际区域
            accessKeyId: responseData.AccessKeyId, // 替换为你的实际 AccessKeyId
            accessKeySecret: responseData.AccessKeySecret, // 替换为你的实际 AccessKeySecret
            stsToken: responseData.SecurityToken, // 替换为你的实际 SecurityToken
            bucket: responseData.bucket ? responseData.bucket :
            'wakebaoai', // 替换为你的实际存储空间名称oss-cn-shanghai.aliyuncs.com
          });
          // const file = this.getFileFromPath(tmpFile.previewUrl); //blob 数据格式
          const file = tmpFile.file; //blob 数据格式
          // const file = tmpFile.previewUrl;
          const options = {
            progress: (p) => {},
            parallel: 4,
            partSize: 1024 * 1024,
            mime: tmpFile.type, //'video/mp4'
          };
          // 分片上传
          await this.client.multipartUpload(name, file, options);
          var savePath = (responseData.url ? responseData.url : that.aliUrl) + "/" + name
         
          //将阿里云路径 保存到数据库------提交
          that.submitAudio(savePath, loading);
        } else {
          that.isOperating = false;
          this.$message(response.message)
        }
      },

就这么简单!

相关推荐
柒和远方1 分钟前
Phase 7.3 复盘:后台任务不只是“扔进队列”,还要能被看见
前端·后端·架构
2501_943782355 分钟前
【共创季稿事节】 倒计时器:时分秒选择器与定时器的协同工作
前端·华为·harmonyos·鸿蒙·鸿蒙系统
奶油mm13 分钟前
公司技术债堆积如山,我一人之力用 Vue3 偷换了整个前端架构
前端·vue.js
用户9385156350714 分钟前
深入理解 JavaScript 中的 this 与数据存储的奥秘
前端·javascript
Tian_Hang25 分钟前
eclipse ditto 学习笔记
运维·服务器·开发语言·javascript·3d
JNX_SEMI1 小时前
AT2659 L1频段多模卫星导航低噪声放大器技术解析
前端·单片机·嵌入式硬件·物联网·硬件工程
Profile排查笔记3 小时前
指纹浏览器环境异常排查:Fingerprint、Profile、Proxy、Session 和 Task Log 怎么看
前端·人工智能·后端·自动化
京韵养生记3 小时前
【无标题】
java·服务器·前端
格子软件3 小时前
2026年分布式GEO代理流量调度:源码级状态机防重挂实战
java·vue.js·人工智能·spring boot·分布式·vue
大气的小蜜蜂3 小时前
领域层的服务
java·前端·数据库