VUE-阿里云OSS上传组件

本文档介绍了如何使用封装的阿里云OSS组件进行图片和视频上传,提供了组件代码示例,涵盖了配置、参数详解和常见操作,适合前端开发者快速集成云存储功能。

根据项目需求封装的一个阿里云OSS上传组件,可进行图片上传和视频上传。

上组件代码:

<template>
  <div>
    <div v-if="uploadType == 'image'">
      <el-upload
        class="avatar-uploader"
        :style="{ width: imgWidth + 'px', height: imgHeight + 'px' }"
        action=""
        :show-file-list="false"
        :auto-upload="false"
        :on-change="(file) => uploadImageOrVideo(file)"
      >
        <img
          v-if="showUrl"
          :src="OSSUrl(showUrl)"
          class="avatar"
          :style="{ width: imgWidth + 'px', height: imgHeight + 'px' }"
        />
        <i
          v-else
          class="el-icon-plus avatar-uploader-icon"
          :style="{
            width: imgWidth + 'px',
            height: imgHeight + 'px',
            'line-height': imgHeight + 'px',
          }"
        ></i>
      </el-upload>
    </div>
    <div v-if="uploadType == 'video'" class="video-main">
      <el-upload
        v-show="!uploadProgress"
        style="display: inline-block"
        action=""
        :multiple="false"
        :auto-upload="false"
        :show-file-list="false"
        :on-change="(file) => uploadImageOrVideo(file)"
      >
        <el-button
          style="margin-left: 10px"
          type="normal"
          size="small"
          @click="uploadBefore"
          >选择视频</el-button
        >
      </el-upload>

      <el-button
        v-if="!showProgress && uploadProgress"
        style="margin-left: 10px"
        type="normal"
        size="small"
        >视频上传中...</el-button
      >
      <div style="width: 150px" v-if="uploadProgress && showProgress">
        <el-progress
          :text-inside="true"
          :stroke-width="20"
          :percentage="uploadProgress"
        ></el-progress>
      </div>
      <el-button
        v-if="uploadProgress && cancelUpload"
        style="margin-left: 10px"
        type="normal"
        size="small"
        @click="stopUpload"
        >取消上传</el-button
      >
      <el-button
        v-show="showUrl && !uploadProgress && videoPreview"
        style="margin-left: 10px"
        type="normal"
        size="small"
        @click="dialogVisible = true"
        >视频预览</el-button
      >
    </div>
    <el-dialog title="视频预览" :visible.sync="dialogVisible" width="650px">
      <video
        controls
        :src="OSSUrl(showUrl)"
        style="width: 600px; height: 400px"
      />
    </el-dialog>
  </div>
</template>
<script>
// 下载oss模块后,引入使用
import OSS from 'ali-oss'
let clientArr = []
export default {
  props: {
    uploadType: {//上传的类型:image-图片(默认),video-视频
      type: String,
      default: 'image'
    },
    showFileUrl: {//要显示的image/video在阿里云服务器上的地址
      type: String,
      default: ''
    },
    configureOSS: {//创建OSS实例所需的配置项: 示例:{region:'oss-cn-shenzhen',accessKeyId:'xxx',accessKeySecret:'xxx',stsToken:'xxx',timeout:'xxx',bucket:'xxx'}
      type: Object,
    },
    // createOSS: {//是否在初始化时创建OSS实例
    //   type: Boolean,
    //   default: false
    // },
    imgWidth: {//图片宽度
      type: String,
      default: '196'
    },
    imgHeight: {//图片高度
      type: String,
      default: '146'
    },
    fileName: {//文件上传名-在阿里云的目录位置
      type: String,
    },
    uploadHeaders: {//文件上传携带头部
      type: Object,
    },
    videoPreview: {//是否开启视频预览
      type: Boolean,
      default: false
    },
    cancelUpload: {//是否开启取消上传功能
      type: Boolean,
      default: false
    },
    showProgress: {//是否显示上传视频的进度条
      type: Boolean,
      default: false
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值