微信小程序开发——上传图片

功能描述

1.拍摄或从手机相册中选择图片上传。

2.chooseImage(e) 中的index用于判断是新增图片还是替换图片。

3.delImage(e) 删除当前index索引下的数据。

wx.chooseMedia(Object object)

   属性类型默认值必填说明
countnumber9最多可以选择的文件个数
mediaTypeArray.<string>['image', 'video']文件类型
sourceTypeArray.<string>['album', 'camera']图片和视频选择的来源
maxDurationnumber10拍摄视频最长拍摄时间,单位秒。时间范围为 3s 至 60s 之间。不限制相册。
sizeTypeArray.<string>['original', 'compressed']仅对 mediaType 为 image 时有效,是否压缩所选文件
camerastring'back'仅在 sourceType 为 camera 时生效,使用前置或后置摄像头
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行

 

 

object.success 回调函数

属性类型说明
tempFilesArray.<Object>本地临时文件列表
结构属性类型说明
tempFilePathstring本地临时文件路径 (本地路径)
sizenumber本地临时文件大小,单位 B
durationnumber视频的时间长度
heightnumber视频的高度
widthnumber视频的宽度
thumbTempFilePathstring视频缩略图临时文件路径
fileTypestring文件类型
合法值说明
image图片
video视频
typestring文件类型,有效值有 image 、video、mix

wxml部分

<view class="imageList">
    <view class="imageItem" wx:for="{{imageList}}" wx:key="index">
        <image class="image"  mode="aspectFill" bindtap="chooseImage"  data-type="pictures"  data-index="{{index}}" src="{{item.url}}"  alt="" />
        <view class="delImage" catchtap="delImage" data-idnex="{{index}}">-</view>
    </view>
    <view class="imageItem addImage" bindtap="chooseImage"  data-type="pictures"></view>
</view>

wxss部分

.imageList {
  padding: 32rpx;
  width: 100vw;
  box-sizing: border-box;

  /* 弹性布局 */
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  /* 换行 */
  flex-wrap: wrap;

  /* 内容区的实际宽度为
   width: calc(100vw - 64rpx); 
   width: 686rpx; 
    */

}

/* 照片容器的样式 */
.imageList .imageItem {
  margin-bottom: 25rpx;
  width: 212rpx;
  height: 212rpx;
  background: #F7F9FD;
  border-radius: 8rpx;
  border: 5rpx dotted #D0D0D0;
  position: relative;
  /* overflow: hidden; */
  box-sizing: border-box;
}

.imageList .imageItem:nth-child(3n-1) {
  margin: 0 25rpx;
}

/* 图片的样式 */
.imageList .imageItem .image {
  display: block;
  width: 100%;
  height: 100%;
}

/* 删除按钮的样式 */
.imageList .imageItem .delImage {
  width: 32rpx;
  height: 32rpx;
  border-radius: 50%;
  background-color: #e40606;
   color: #ffffff;
  font-size: 32rpx;
  text-align: center;
  line-height: 22rpx;
  position: absolute;
  top: -16rpx;
  right: -16rpx;

  box-sizing: border-box;
}

/* 可以使用背景图 */
.imageList .addImage::before {
  display: block;
  content: "+";
  width: 100rpx;
  height: 100rpx;
  color: #cccccc;
  /* font-weight: 700; */
  font-size: 70rpx;
  text-align: center;
  line-height: 100rpx;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

js部分

Page({
  data: {
    imageList: []
  },
  //选择图片
  chooseImage(e) {
    let index=e.currentTarget.dataset.index
    console.log(index)
    let self = this
    wx.chooseMedia({
      count: 9,
      sizeType: ['original', 'compressed'], //原图 ,压缩图
      sourceType: ['album', 'camera'], //从相处选择 ,使用相机
      success(res) {
        console.log("res___________",res)
        res.tempFiles.forEach((file) => {
          if(index === undefined){ //添加图片
            self.setData({
              imageList: [...self.data.imageList, {
                url: file.tempFilePath
              }]
            })
          }else{ //替换当前索引下的图片
            self.data.imageList[index].url=file.tempFilePath
            self.setData({
              imageList:self.data.imageList
            })
          }
        })
      }
    })
  },
  //删除图片
  delImage(e) {
    let {
      imageList
    } = this.data
    let index = e.currentTarget.dataset.index
    imageList.splice(index, 1)
    this.setData({
      imageList
    })
  }
})

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值