坑3.上传图片(阿里云空间,oss验证)(未验证)

笔记

20240710 未验证,现在还没有阿里云空间,等买个sit环境就可以验证一下。

前端

页面
html 复制代码
<!--页面-->
<el-form-item label="优惠券图片" prop="couponImg">
    <single-upload v-model="dataForm.couponImg"></single-upload>
</el-form-item>

<!--vue引入-->
import SingleUpload from "@/components/upload/singleUpload";


<!--vue对象里加组件-->
components: { SingleUpload },

<!--保存方法,couponImg就是url路径-->
组件
html 复制代码
<template> 
  <div>
    <!-- action必选参数,上传地址,bucket的外网访问域名 -->
    <el-upload
      action="https://vivi-gulimall.oss-cn-hangzhou.aliyuncs.com"
      :data="dataObj"
      list-type="picture"
      :multiple="false" :show-file-list="showFileList"
      :file-list="fileList"
      :before-upload="beforeUpload"
      :on-remove="handleRemove"
      :on-success="handleUploadSuccess"
      :on-preview="handlePreview">
      <el-button size="small" type="primary">点击上传</el-button>
      <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过10MB</div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="fileList[0].url" alt="">
    </el-dialog>
  </div>
</template>
<script>
   import {policy} from './policy'
   import { getUUID } from '@/utils'

  export default {
    name: 'singleUpload',
    props: {
      value: String
    },
    computed: {
      imageUrl() {
        return this.value;
      },
      imageName() {
        if (this.value != null && this.value !== '') {
          return this.value.substr(this.value.lastIndexOf("/") + 1);
        } else {
          return null;
        }
      },
      fileList() {
        return [{
          name: this.imageName,
          url: this.imageUrl
        }]
      },
      showFileList: {
        get: function () {
          return this.value !== null && this.value !== ''&& this.value!==undefined;
        },
        set: function (newValue) {
        }
      }
    },
    data() {
      return {
        dataObj: {
          policy: '',
          signature: '',
          key: '',
          ossaccessKeyId: '',
          dir: '',
          host: '',
          // callback:'',
        },
        dialogVisible: false
      };
    },
    methods: {
      emitInput(val) {
        this.$emit('input', val)
      },
      handleRemove(file, fileList) {
        this.emitInput('');
      },
      handlePreview(file) {
        this.dialogVisible = true;
      },
      beforeUpload(file) {
        let _self = this;
        return new Promise((resolve, reject) => {
          policy().then(response => {
            _self.dataObj.policy = response.data.policy;
            _self.dataObj.signature = response.data.signature;
            _self.dataObj.ossaccessKeyId = response.data.accessid;
            _self.dataObj.key = response.data.dir + getUUID() + '_${filename}';
            _self.dataObj.dir = response.data.dir;
            _self.dataObj.host = response.data.host;
            console.log("上传前请求服务端签名,得到结果:", _self.dataObj)
            resolve(true)
          }).catch(err => {
            reject(false)
          })
        })
      },
      handleUploadSuccess(res, file) {
        console.log("上传成功...")
        this.showFileList = true;
        this.fileList.pop();
        this.fileList.push({name: file.name, url: this.dataObj.host + '/' + this.dataObj.key.replace("${filename}",file.name) });
        this.emitInput(this.fileList[0].url);
      }
    }
  }
</script>
<style>

</style>
相关推荐
土司大王16 分钟前
LeetCode hto100——字母异位词分组
java·算法·leetcode
码匠许师傅27 分钟前
【C++ 面试真题】聊聊 C++ 中的 lambda 表达式
java·c++·面试
QCodingDev1 小时前
Spring AI Alibaba ReAct Agent实战:从Tool Calling到Agent,企业AI复杂业务该如何设计?
java·人工智能·agent·ai编程·spring ai
RainCity1 小时前
Java Swing 自定义组件库分享(十六)
java·笔记·后端
赵广陆2 小时前
企业实战:Markdown图片检索
android·java·开发语言
C++、Java和Python的菜鸟2 小时前
第3章 从0开始用若依
java·开发语言
Json____3 小时前
五金制品行业-企业官网源码
java·大数据·数据库·企业站·wwwoop.com
counting money4 小时前
Spring AI Alibaba 从入门到实战:构建企业级 AI 应用
java·人工智能·spring
原野池予4 小时前
深入Java集合框架:数组与List互转底层原理剖析(JDK 8)
java·数据结构·list
Java成神之路-5 小时前
深入理解 HashMap 扩容时机:JDK 7 vs JDK 8 的设计差异与取舍
java·juc