Oss文件上传-简化版

这里使用的是element的上传组件,相关上传钩子看官网

一、获取到oss后台相关的签名数据

建议通过接口获取

 二、上传

 //获取oss相关参数
    // getOsstoken
    async beforeUploadMasterImg(file) {
      console.log("点击上传");
      await this.getOssSingnature(file);
    },
    async getOssSingnature(file) {
      var _this = this;
      console.log("开始准备上传");
      await getOsstoken({ dir: "xxx/xxx/xx/" }).then((res) => {
        console.log("获取上传地址");
        console.log(res);
        _this.pageUpObj.policy = res.data.policy;
        _this.pageUpObj.dir = res.data.dir + "xxx";//oss存储文件夹
        _this.pageUpObj.OSSAccessKeyId = res.data.accessId; 
        _this.pageUpObj.host = res.data.host;
        _this.pageUpObj.key = res.data.dir + file.uid + "_" + file.name; //文件命名唯一值 后续url拼接使用
        _this.pageUpObj.signature = res.data.signature;
      });
    },
    uploadSuccess() {
      console.log('上传成功获得url');
      const MSGurl = this.pageUpObj.host + "/" + this.pageUpObj.key;
     
    },

20230816--修复下载失败功能

原因:host地址为不带https的,需要手动设置为https://

 _this.pageUpObj.host ='https://'+ res.data.host.split('//')[1];

猜你喜欢

转载自blog.csdn.net/a99101/article/details/131766094