• Hbuilder中微信小程序上传多图的案例分享


    1、实现效果图如下:

     描叙:最后那个加号+是上传图片的按钮。每张图片右上角有删除按钮。

    我这里限定每行显示三张图片。

    2、具体代码如下:

    页面data()中定义一个待上传图片的数组变量 tempFiles:[],

    页面View内容:

    1. <view class="car-list">
    2. <view class="car-item" v-for="(item2, index2) in tempFiles" :key="index2">
    3. <view :style="{'background-image':'url('+item2.tempFilePath+')','background-repeat':'no-repeat','background-size':'98px 98px'}">
    4. <image src="/static/img/c/delete.png" mode="aspectFit" class="deleteImg" @click="deleteCarPhoto(index2)"></image>
    5. </view>
    6. </view>
    7. <view class="car-item">
    8. <view style="background-image:url('/static/img/personal/add.png');background-repeat:no-repeat;background-size:98px 98px;" @tap="selectCarPhoto()">
    9. </view>
    10. </view>
    11. </view>

    car-list 下最后一个car-item 是我们的上传图片添加按钮。

    v-for 循环 tempFiles 图片。

    methods z中定义添加图片和删除图片的方法:

    1. //添加图片
    2. selectCarPhoto(){
    3. wx.chooseMedia({
    4. count: 5,
    5. mediaType: ['image'],
    6. sourceType: ['album', 'camera'],
    7. maxDuration: 30,
    8. camera: 'back',
    9. success: (res) => {
    10. console.log("选择图片成功:"+JSON.stringify(res));
    11. if(res.errMsg=="chooseMedia:ok"){
    12. this.tempFiles=this.tempFiles.concat(res.tempFiles);
    13. }else{
    14. uni.showToast({
    15. title: res.errMsg,
    16. icon: "none",
    17. position: 'center',
    18. duration: 3000
    19. });
    20. }
    21. },fail: (res) => {
    22. console.log("选择图片失败:"+res.errMsg);
    23. }
    24. })
    25. },
    26. //删除图片
    27. deleteCarPhoto(index){
    28. this.tempFiles.splice(index,1);
    29. },

    3、页面的CSS样式(此处样式可以根据自己需求进行调整)

    1. <style>
    2. .car-list{
    3. display: flex;
    4. justify-content: space-between;
    5. align-items: center;
    6. flex-wrap: wrap;
    7. margin-top:10px;
    8. }
    9. .car-item {
    10. flex: 1;
    11. text-align: center;
    12. margin: 0 10px 10px 0;
    13. box-sizing: border-box;
    14. }
    15. .car-item view{
    16. width:100px;
    17. height: 100px;
    18. }
    19. .car-item:nth-child(3n + 3) {
    20. margin-right: 0;
    21. }
    22. .deleteImg {
    23. width: 40rpx !important;
    24. height: 40rpx !important;
    25. margin-left: 70px;
    26. }
    27. </style>

    4、微信上传文件的方法

    注:wx.uploadFile 方法需要小程序设置,上传文件合法域名才可使用。本地测试时,微信开发者工具可以选择不校验合法域名。

    1. uploadCarPhoto(carId){
    2. //上传
    3. console.log("开始上传图片");
    4. uni.showLoading({
    5. title: '正在上传车辆图片中......'
    6. });
    7. var fileLength=this.tempFiles.length;
    8. //这个参数是我自定义的,为了最后确定是否都上传完毕,因为微信官方文档并没有多图上传功能
    9. var orderNum=0;
    10. for (var i = 1; i <= this.tempFiles.length; i++) {
    11. let tempFilePath = this.tempFiles[i-1].tempFilePath;
    12. wx.uploadFile({
    13. url: Common.PROJECT_URL+'/context/XXX/uploadCarPhoto',
    14. filePath: tempFilePath,
    15. name: 'car_file',
    16. formData: {
    17. param1: carId,
    18. orderNum: i
    19. },
    20. success(res) {
    21. orderNum=orderNum+1;
    22. if(res.statusCode==200){
    23. var ret=JSON.parse(res.data);
    24. if (ret.code == 1) {
    25. if(orderNum==fileLength){
    26. uni.hideLoading();
    27. wx.showToast({
    28. title: '保存成功',
    29. duration: 2000
    30. });
    31. uni.reLaunch({
    32. url:"/pages/index/index"
    33. });
    34. }
    35. }else{
    36. uni.hideLoading();
    37. uni.showToast({
    38. title: ret.message,
    39. icon: "none",
    40. position: 'bottom',
    41. duration: 3000
    42. });
    43. }
    44. }else{
    45. uni.hideLoading();
    46. uni.showToast({
    47. title: res.errMsg,
    48. icon: "none",
    49. position: 'bottom',
    50. duration: 3000
    51. });
    52. }
    53. }
    54. });
    55. }
    56. }

    关于微信小程序多图上传,各位大佬还有没有更好的方式呢?欢迎评论区留言,我们共同探讨。

  • 相关阅读:
    React---简单原理
    力扣记录:动态规划4股票问题——121,122,123,188 ,309,714买卖股票的最佳时机(I,II,III,IV,含冷冻期,含手续费)
    Redis集群操作-----主从互换
    企业集中监控体系思路及架构
    移动端适配单位vw和px的转换
    艺术与科技的结合,AI绘画图生图怎么样?
    下载离线地图地形数据库(3D离线地图开发)
    Lumiprobe 蛋白质标记研究方案
    篇1:Mapbox Style Specification
    【正则表达式】正则表达式里使用变量
  • 原文地址:https://blog.csdn.net/weixin_36754290/article/details/125567416