小程序上传又拍云

时间:2024-04-09 15:37:49

官方文档参考https://help.upyun.com/knowledge-base/small_program/

小程序上传图片到又拍云不同于h5上传可以用input action="http://v0.api.upyun.com/miandan"方法

小程序注意要素

1、注意小程序配置安全域名

2、注意以下参数的配置,一定要正确

const upyun = new Upyun({ 

  bucket: '服务名称',

  operator: '操作员',

  getSignatureUrl: '获取signature的接口地址'

})

小程序上传又拍云

3、注意文件保存路径问题remotePath的值可自定义,不需在又拍云后台配置文件夹

注意文件保存路径格式 '/ypy/image' + year + month + day + hour + minutes + second + millisecond + rnd + '.png' 

'/ypy/image' 为可自定义,时间戳为图片名称设置 、格式为png、

chooseImage: function () {

console.log('chooseImage')

const self = this

 

wx.chooseImage({

count: 1,

sizeType: ['compressed'],

sourceType: ['album'],

success: function (res) {

console.log('chooseImage success, temp path is', res.tempFilePaths[0])

 

const imageSrc = res.tempFilePaths[0]

var myDate = new Date();

var year = myDate.getFullYear();

var month = myDate.getMonth()+1;

var day = myDate.getDate();

var hour = myDate.getHours();

var minutes = myDate.getMinutes();

var second = myDate.getSeconds();

var millisecond = myDate.getMilliseconds();

var rnd = Math.floor(Math.random()*100000);

upyun.upload({

localPath: imageSrc,

remotePath: '/ypy/image' + year + month + day + hour + minutes + second + millisecond + rnd + '.png' ,

success: function (res) {

console.log('uploadImage success, res is:', res)

wx.showToast({

title: '上传成功',

icon: 'success',

duration: 1000

})

 

self.setData({

imageSrc

})

},

fail: function ({errMsg}) {

console.log('uploadImage fail, errMsg is', errMsg)

}

})

},

fail: function ({errMsg}) {

console.log('chooseImage fail, err is', errMsg)

}

})

}