当前位置: 首页>編程日記>正文

微信小程序图片上传组件

微信小程序图片上传组件

参考:微信小程序图片上传组件 - 简书

                                                                         gif效果图,加载需要一丢丢时间

功能介绍
1.点击+框,选择并展示所选图片
2点击×删除对应图片
3.右上角0/9看到没,最多选择9张
4.点击图片放大预览,并可左右滑动切换

先来介绍一下各项数据

Page({data: {uploaderList: [],uploaderNum:0,showUpload:true}
})

uploaderList:已选择的图片临时路径数组

uploaderNum:已选择图片个数

showUpload:用来判断是否可继续选择图片,当传至9张时不可继续上传

界面布局

<!-- 容器 -->
<view class='ui_uploader_cell'><!-- 根据已选择的图片临时路径数组展示图片--><view class='ui_uploader_item' wx:for="{{uploaderList}}" wx:key="{{index}}"><!-- 删除--><icon class='ui_uploader_item_icon' bindtap='clearImg' data-index="{{index}}" type="clear" size="20" color="red"/><!-- 图片--><image bindtap='showImg' data-index="{{index}}" src='{{item}}'></image></view><!-- 上传按钮+框 --><view class='ui_uploader' bindtap='upload' wx:if="{{showUpload}}"></view>
</view>

根据已选择的图片临时路径数组展示图片wx:for="{{uploaderList}}"
删除icon上挂载data数据用来删除指定图片data-index="{{index}}"
图片上挂载data数据用来点击放大展示图片data-index="{{index}}"
根据已选图片个数判断是否可继续上传wx:if="{{showUpload}}"

逻辑处理

//index.js
//获取应用实例
const app = getApp()
Page({data: {uploaderList: [],uploaderNum:0,showUpload:true},// 删除图片clearImg:function(e){var nowList = [];//新数据var uploaderList = this.data.uploaderList;//原数据for (let i = 0; i < uploaderList.length;i++){if (i == e.currentTarget.dataset.index){continue;}else{nowList.push(uploaderList[i])}}this.setData({uploaderNum: this.data.uploaderNum - 1,uploaderList: nowList,showUpload: true})},//展示图片showImg:function(e){var that=this;wx.previewImage({urls: that.data.uploaderList,current: that.data.uploaderList[e.currentTarget.dataset.index]})},//上传图片upload: function(e) {var that = this;wx.chooseImage({count: 9 - that.data.uploaderNum, // 默认9sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有success: function(res) {console.log(res)// 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片let tempFilePaths = res.tempFilePaths;let uploaderList = that.data.uploaderList.concat(tempFilePaths);if (uploaderList.length==9){that.setData({showUpload:false})}that.setData({uploaderList: uploaderList,uploaderNum: uploaderList.length,})}})},onLoad: function() {}
})

以下是我自己的线上版本:

上传图片组件:

index.wxml

<view class="panel-title">{{title}}</view><view class="{{uploaderList.length === 0 ? 'ui-uploader-cell':'ui-uploader-cell-other'}}"><!-- 根据已选择的图片临时路径数组展示图片--><view class='ui-uploader-item' wx:for="{{uploaderList}}" wx:key="{{index}}"><!-- 删除--><icon class='ui-uploader-item-icon' bindtap='clearImg' data-index="{{index}}" type="clear" size="20" color="#666666" /><!-- 图片--><image bindtap='showImg' data-index="{{index}}" src='{{item}}'></image></view><!-- 上传按钮+框 --><view class='ui-uploader' bindtap='upload' wx:if="{{showUpload}}"><image src="/images/update_img.png"></image></view><view wx:if="{{uploaderList.length === 0}}" class="title2">{{desc}}</view></view>

index.wxss

@import '/app.wxss';
.panel-title{margin-bottom: 20rpx;
}
.ui-uploader-cell-other {width: 690rpx;padding: 40rpx 0;display: flex;flex-wrap: wrap;align-content: flex-start;/* justify-content: flex-start; *//* align-items: center; */
}.ui-uploader-cell {width: 690rpx;/* padding: 20rpx 0; */display: flex;/* justify-content: flex-start; */flex-wrap: wrap;align-content: flex-start;
}.ui-uploader-item {width: 118rpx;height: 118rpx;margin-right: 18rpx;margin-bottom: 18rpx;padding-bottom: 2rpx;padding-right: 2rpx;position: relative;
}.title2 {width: 520rpx;display: flex;align-items: center;
}.ui-uploader-item-icon {position: absolute;right: 0;
}.ui-uploader {width: 120rpx;height: 120rpx;margin-right: 20rpx;background: #fff;border-radius: 16rpx;display: flex;justify-content: center;align-items: center;box-shadow: 0 0rpx 40rpx 0 rgba(0, 0, 0, 0.08);
}

index.js

// components/case-imgs/index.js
Component({/*** 组件的属性列表*/properties: {uploaderList: {type: Array,value: []},uploaderNum: {type: Number,value: 0},desc: {type: String,value: '拍照化验单、检查资料报告单、药品处方单、患处照片等。'},title: {type: String,value: '补充图片'},type: {// false 开启图文咨询  补充咨询type: Boolean,value: false}},/*** 组件的初始数据*/data: {uploaderList: [],uploaderNum: 0,showUpload: true,uploaderNowNum:0},/*** 组件的方法列表*/methods: {// 删除图片clearImg: function(e) {var nowList = []; //新数据var uploaderList = this.data.uploaderList; //原数据for (let i = 0; i < uploaderList.length; i++) {if (i == e.currentTarget.dataset.index) {continue;} else {nowList.push(uploaderList[i])continue;}}this.setData({uploaderNum: this.data.uploaderNum - 1,uploaderList: nowList,showUpload: true})this.triggerEvent('getUploaderList', {uploaderList: nowList})},//展示图片showImg: function(e) {var that = this;wx.previewImage({urls: that.data.uploaderList,current: that.data.uploaderList[e.currentTarget.dataset.index]})},//上传图片upload: function(e) {var that = this;wx.chooseImage({count: 5 - that.properties.uploaderNum, // 默认5sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有success: function(res) {console.log(res)// 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片let tempFilePaths = res.tempFilePaths;console.log(tempFilePaths)let uploaderList = that.data.uploaderList.concat(tempFilePaths);if (!that.properties.type) {//开启图文咨询if (uploaderList.length == 5) {that.setData({showUpload: false})}that.setData({uploaderList: uploaderList,uploaderNum: uploaderList.length,})that.triggerEvent('getUploaderList', {uploaderList: uploaderList})}else{// 补充咨询if (uploaderList.length + that.properties.uploaderNum === 5) {that.setData({showUpload: false})}that.setData({uploaderList: uploaderList,uploaderNowNum: uploaderList.length + that.properties.uploaderNum})that.triggerEvent('getUploaderList', {uploaderList: uploaderList,uploaderNowNum: that.data.uploaderNowNum})}}})},}
})

页面调用组件:

 <uploads-img title="上传资料" bind:getUploaderList="getUploaderList"></uploads-img>
  //获取上传图片getUploaderList(e) {this.setData({uploaderList: e.detail.uploaderList})},


https://www.fengoutiyan.com/post/16166.html

相关文章:

  • 怎么把小程序码放在图片上
  • 小程序上传图片接口
  • 如何将照片传到一个小程序
  • 小程序怎么添加在图片上
  • 什么小程序可以上传图片
  • 图片上加小程序
  • 小程序图片预加载如何实现
  • 微信小程序一直加载中
  • 鏡像模式如何設置在哪,圖片鏡像操作
  • 什么軟件可以把圖片鏡像翻轉,C#圖片處理 解決左右鏡像相反(旋轉圖片)
  • 手機照片鏡像翻轉,C#圖像鏡像
  • 視頻鏡像翻轉軟件,python圖片鏡像翻轉_python中鏡像實現方法
  • 什么軟件可以把圖片鏡像翻轉,利用PS實現圖片的鏡像處理
  • 照片鏡像翻轉app,java實現圖片鏡像翻轉
  • 什么軟件可以把圖片鏡像翻轉,python圖片鏡像翻轉_python圖像處理之鏡像實現方法
  • matlab下載,matlab如何鏡像處理圖片,matlab實現圖像鏡像
  • 圖片鏡像翻轉,MATLAB:鏡像圖片
  • 鏡像翻轉圖片的軟件,圖像處理:實現圖片鏡像(基于python)
  • canvas可畫,JavaScript - canvas - 鏡像圖片
  • 圖片鏡像翻轉,UGUI優化:使用鏡像圖片
  • Codeforces,CodeForces 1253C
  • MySQL下載安裝,Mysql ERROR: 1253 解決方法
  • 勝利大逃亡英雄逃亡方案,HDU - 1253 勝利大逃亡 BFS
  • 大一c語言期末考試試題及答案匯總,電大計算機C語言1253,1253《C語言程序設計》電大期末精彩試題及其問題詳解
  • lu求解線性方程組,P1253 [yLOI2018] 扶蘇的問題 (線段樹)
  • c語言程序設計基礎題庫,1253號C語言程序設計試題,2016年1月試卷號1253C語言程序設計A.pdf
  • 信奧賽一本通官網,【信奧賽一本通】1253:抓住那頭牛(詳細代碼)
  • c語言程序設計1253,1253c語言程序設計a(2010年1月)
  • 勝利大逃亡英雄逃亡方案,BFS——1253 勝利大逃亡
  • 直流電壓測量模塊,IM1253B交直流電能計量模塊(艾銳達光電)
  • c語言程序設計第三版課后答案,【渝粵題庫】國家開放大學2021春1253C語言程序設計答案
  • 18轉換為二進制,1253. 將數字轉換為16進制
  • light-emitting diode,LightOJ-1253 Misere Nim
  • masterroyale魔改版,1253 Dungeon Master
  • codeformer官網中文版,codeforces.1253 B
  • c語言程序設計考研真題及答案,2020C語言程序設計1253,1253計算機科學與技術專業C語言程序設計A科目2020年09月國家開 放大學(中央廣播電視大學)
  • c語言程序設計基礎題庫,1253本科2016c語言程序設計試題,1253電大《C語言程序設計A》試題和答案200901
  • 肇事逃逸車輛無法聯系到車主怎么辦,1253尋找肇事司機