微信小程序云开发上传图片和删除图片

将图片上传到云开发中的云存储

上传图片

wxml文件

<van-button type="primary" bindtap="afterRead">上传图片</van-button>
<van-image width="100" height="100" src="{{imgUrl}}" />

方法:
先调用 wx.chooseImage()方法,选择图片
然后调用wx.cloud.uploadFile()方法,上传图片到云存储中,并返回图片的fileID

js文件中

    // 选择图片
    afterRead(){
        let that = this
        if(that.data.imgUrl){
            wx.cloud.deleteFile({
                fileList: [that.data.imgUrl]
              }).then(res => {
                // handle success
                console.log("删除成功")
              }).catch(error => {
                // handle error
              })
        }
        wx.chooseImage({
          count: 1,
          sizeType:['original', 'compressed'],
          sourceType:['album', 'camera'],
          success (res) {
            // tempFilePath可以作为 img 标签的 src 属性显示图片
            that.uplodImage(res.tempFilePaths[0])
          }
        })
    },
    // 上传图片
    uplodImage(fileURL){
        wx.cloud.uploadFile({
            cloudPath:new Date().getTime() + '.png',
            filePath:fileURL,
            success:res=>{
                this.setData({
                    imgUrl:res.fileID
                })
            },
            fail:err=>{
                console.log("失败");
            }
        })
    },

删除图片
调用wx.cloud.deleteFile()方法,传入图片的fileID

        wx.cloud.deleteFile({
                fileList: [that.data.imgUrl]
              }).then(res => {
                // handle success
                console.log("删除成功")
              }).catch(error => {
                // handle error
              })
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值