微信小程序云开发-上传图片实例

话不多说上布局
wxml

				<view class="ple">
					<view class="ple-k" style="margin-left:0" bindtap="chooseimg">
						<block wx:if="{{upsrcfile==''}}">
							<image src="/images/pic.png"></image>
						</block>
						<block wx:else>
							<image src="{{upsrcfile}}" style="width:100%;height:100%;margin-top:0"></image>
						</block>
					</view>
					<view class="clear"></view>
				</view>

wxss

.ple{width: 100%;margin-top:calc(30vmin / 7.5);}
.ple-k{width: 45%;border: 1px solid #eee;text-align: center;height:calc(400vmin / 7.5);margin-left: 9%;float: left; }
.ple-k image{width:calc(150vmin / 7.5); height: calc(150vmin / 7.5);margin-top:calc(125vmin / 7.5) ;}

js文件
点击事件设置
使用微信自带api wx.chooseImage 选择图片

  chooseimg: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        wx.showLoading({
          title: '上传中...',
          mask: true
        })
        //返回临时路径
        const filePath = res.tempFilePaths[0]
        var timestamp = Date.parse(new Date());
        //生成时间戳
        const cloudPath = "upvideo/" + timestamp + filePath.match(/\.[^.]+?$/)[0]
        //设置云开发存储图片地址
        //使用wx.cloud 调用云开发上传图片api 参数文件存放地址,图片路径
        wx.cloud.uploadFile({
          cloudPath,
          filePath,
          success: function (res) {
            console.log(res)
            wx.hideLoading();
            that.setData({
              upsrcfile: res.fileID
            })
          }
        })
      }
    })
  },

上传视频 使用 wx.chooseVideo api选择视频 使用wx.cloud.uploadFile文件上传方法,以此类推

谢谢观看,留个关注吧
后面会更新更多实例

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员-南

你的鼓励将是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值