Vue+Element 直传阿里云OSS上传

1.在阿里云控制设置跨域
参考官方文档
https://www.alibabacloud.com/help/zh/doc-detail/32069.htm?spm=a2c63.p38356.b99.394.492b5738ZBBcex
跨域设置
2.新建fileUtils.js

let OSS = require('ali-oss');

let client = new OSS({
    accessKeyId: "",
    accessKeySecret: "",   // 你创建的Bucket时获取的
    bucket: '',  // 你创建的Bucket名称
    region: '',   //  你所购买oss服务的区域,默认oss-cn-hangzhou
    timeout:60000,//超时时间 默认60S 自行设置
});

// 上传  两种方式 multipartUpload=>分片上传  put=>整体上传
export async function multipartUpload(filePath, file) {
    // console.log(filePath)
    // console.log(file)
    try {
        // console.log(file.size)
        let result = await client.multipartUpload(filePath, file, {
            parallel: 4,// 同时请求分片数
            partSize: 1024 * 1024,// 分片大小
            // progress: function (p, cpt, res) {
            // 进度条
            //   console.log(p);
            // //   console.log(cpt);
            //   console.log(res.headers['x-oss-request-id']);
            // }
          })
        result.res.imgSize=file.size
        return result 
    } catch (err) {
        console.log(err)
    }
}

// 删除
export async function remove(filePath) {
    try {
        let result = await client.delete(filePath)
        return result
    } catch (err) {
        console.log(err)
    }
}

3、上传页面使用

import { client, multipartUpload, remove } from "../../request/fileUtils";
// item 图片信信息
    upLoad(item) {
      let that = this;
      var fileName = item.uid;
      var date = new Date();
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      month = month < 10 ? "0" + month : month;
      var mydate = date.getDate();
      mydate = mydate < 10 ? "0" + mydate : mydate;
      this.baseurl = "uploads/" + year + month + mydate + "/"; //上传到阿里云指定路径
      var filePath = this.baseurl + fileName;//  名字
      var file = item.raw;
      multipartUpload(filePath, file).then(result => {
        let t = result;
       console.log(result)
      });
    },
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值