小程序上传图片到阿里云oss

首先到阿里云下载文档示例

https://help.aliyun.com/document_detail/31925.html?spm=a2c4g.11186623.6.634.CwkAvp,解压后里面有个upload.js文件,编辑这个文件,把accessid、accesskey、host换成自己的,由于小程序只接受https,所以host最好是绑定自己的域名,且实用了https,然后找到new_multipart_params,使用log打印出来,接下来运行一遍index.html,上传图片后会打印出我们需要的东西



接下来就小程序上传了,使用

chooseImage和uploadFile


uploadImage: function () {
    var that = this
    wx.chooseImage({
      count: 3, // 默认9
      sizeType: ['original'], // 可以指定是原图还是压缩图,默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        var myDate = new Date()
        var ossPath = 'seekings/' + myDate.getFullYear()
        for (var i = 0; i < tempFilePaths.length; i++) {
          // 获取文件后缀
          var pathArr = tempFilePaths[i].split('.')
          //  随机生成文件名称
          var fileRandName = Date.now() + "" + parseInt(Math.random() * 1000)
          var fileName = fileRandName + '.' + pathArr[3]
          // 要提交的key
          var fileKey = ossPath + '/' + fileName
          wx.uploadFile({
            url: 'https://xxx.xxx.com', 
            filePath: tempFilePaths[i],
            name: 'file',
            formData: {
              name: tempFilePaths[i],
              key: fileKey,
              policy: 'xxxxxxxxxx',
              OSSAccessKeyId: 'xxxxxxx',
              signature: 'xxxxxx',
              success_action_status: "200"
            },
            success: function (res) {
              var data = res.data
              console.log(res)
            }
          })
        }
        that.setData({
          upliadImages: res.tempFilePaths
        })
      }
    })
  },

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值