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

 笔记

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

 前端

        页面
<!--页面-->
<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路径-->
        组件
<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>


相关推荐

  1. 3.图片阿里空间oss验证

    2024-07-11 06:14:04       20 阅读
  2. 用php图片阿里oss

    2024-07-11 06:14:04       55 阅读
  3. 后端文件使用阿里oss存储文件(图片

    2024-07-11 06:14:04       30 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 06:14:04       53 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 06:14:04       55 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 06:14:04       46 阅读
  4. Python语言-面向对象

    2024-07-11 06:14:04       56 阅读

热门阅读

  1. Flutter RSA公钥转PEM

    2024-07-11 06:14:04       21 阅读
  2. CentOS 系统监控项

    2024-07-11 06:14:04       19 阅读
  3. UCOS-III 与UCOS-III主要功能差异

    2024-07-11 06:14:04       14 阅读
  4. 用 adb 来模拟手机插上电源和拔掉电源的情形

    2024-07-11 06:14:04       19 阅读
  5. OpenResty程序如何连接开启了TLS的Redis?

    2024-07-11 06:14:04       22 阅读
  6. Jitsi Meet指定用户成为主持人

    2024-07-11 06:14:04       17 阅读
  7. Rust编程-编写自动化测试

    2024-07-11 06:14:04       24 阅读
  8. 开源大势所趋

    2024-07-11 06:14:04       21 阅读
  9. Sqlmap中文使用手册 - Target模块参数使用

    2024-07-11 06:14:04       23 阅读