• Vue 将本地图片上传到阿里云


    一、获取服务器通行证(即获取AccessKey和accessKeySecret)

        getAccess () {
            let that = this
            let url = '服务器地址'
            let params = {
              type: 'K'
            }
            // 第一步:获取AccessKey
            this.$api.send(url, params).then((response) => {
              if (response.status === 200) {
                that.accessKey = response.body.data.data.Credentials
                /* global OSS */
                that.client = new OSS.Wrapper({
                  region: 'oss-cn-shenzhen',
                  bucket: '阿里云bucket文件夹名',
                  accessKeyId: that.accessKey.AccessKeyId,
                  accessKeySecret: that.accessKey.AccessKeySecret,
                  stsToken: that.accessKey.SecurityToken
                })
                that.folder = response.body.data.data.folder;
                for (let i = 0; i < that.allList.length; i++) {
                  if (!that.allList[i].hasUpload) {//allList 需要上传的图片数组
    (function () { that.uploadItem(that.allList[i], i) })(i) } } } }) }

    二、上传到服务器

          uploadItem (file, index) {
            let that = this
            let progress = function (p) {
              return function (done) {
                done()
              }
            }
            // 命名规则:web+id+10位时间戳+随机4位数
            let storeAs = 'web' + that.$user.getAll().userId + Math.round(new Date().getTime() / 1000) + Math.ceil(Math.random() * 100000).toString() + '.' + file.name.split('.')[1]
            // 第二步:调用阿里云上传函数上传文件
            this.client.multipartUpload(this.folder + '/' + storeAs, file.obj, {
              progress: progress
            }).then(function (result) {
              alert(上传成功)
            }).catch(function (err) {
              console.log(err)
            })
          }
     this.getUnloadImg()
    

      

  • 相关阅读:
    <转>iOS SDK中使用NSXMLParser解析XML
    [原创]闲来无事,写了个c#的数据库附加工具,现附上源代码
    [转载]php数组操作foreach、each、reset、list
    [原创]xml序列化
    [原创]一个简单的药店用的会员积分管理系统
    [原创]我的cms项目
    简洁、标准的对联广告代码
    C#混淆 xenocode使用说明
    .net 随机数
    [原创]c# as用法
  • 原文地址:https://www.cnblogs.com/candy-Yao/p/9182571.html
Copyright © 2020-2023  润新知