当前位置:   article > 正文

【微信小程序】实现多图上传、预览_微信小程序上传图片显示

微信小程序上传图片显示

wxml 

  1. <view class="weui-cell"> <view class="weui-cell__bd">
  2. <view class="weui-uploader">
  3. <view class="weui-uploader__hd">
  4. <view class="weui-uploader__title">点击可预览选好的图片</view>
  5. <view class="weui-uploader__info">{{pics.length}}/9</view>
  6. </view>
  7. <view class="weui-uploader__bd">
  8. <view class="weui-uploader__files">
  9. <block wx:for="{{pics}}" wx:for-item="image">
  10. <view class="weui-uploader__file">
  11. <image class="weui-uploader__img" src="{{image}}" data-src="{{image}}" bindtap="previewImage"></image>
  12. </view>
  13. </block>
  14. </view>
  15.      <view class="delete-btn" data-index="{{index}}" catchtap="deleteImg"></view>
  16. <!-- isShow 这个是判断是否进行触发点隐藏操作 -->
  17. <view class="weui-uploader__input-box {{isShow?'true':'hideTrue'}}">
  18. <view class="weui-uploader__input" bindtap="chooseImage"></view>
  19. </view>
  20. </view>
  21. </view>
  22. </view>

wxss

  1. page {
  2. line-height: 1.6;
  3. font-family: -apple-system-font, "Helvetica Neue", sans-serif;
  4. }
  5. icon {
  6. vertical-align: middle;
  7. }
  8. .weui-cell {
  9. padding: 10px 15px;
  10. position: relative;
  11. display: -webkit-box;
  12. display: -webkit-flex;
  13. display: flex;
  14. align-items: center;
  15. }
  16. .weui-cell_input {
  17. padding-top: 0;
  18. padding-bottom: 0;
  19. }
  20. .weui-uploader__hd {
  21. display: -webkit-box;
  22. display: -webkit-flex;
  23. display: flex;
  24. padding-bottom: 10px;
  25. align-items: center;
  26. }
  27. .weui-uploader__title {
  28. flex: 1;
  29. }
  30. .weui-uploader__info {
  31. color: #b2b2b2;
  32. }
  33. .weui-uploader__bd {
  34. margin-bottom: -4px;
  35. margin-right: -9px;
  36. overflow: hidden;
  37. }
  38. .weui-uploader__file {
  39. float: left;
  40. margin-right: 9px;
  41. margin-bottom: 9px;
  42. }
  43. .weui-uploader__img {
  44. display: block;
  45. width: 79px;
  46. height: 79px;
  47. }
  48. .weui-uploader__input-box {
  49. float: left;
  50. position: relative;
  51. margin-right: 9px;
  52. margin-bottom: 9px;
  53. width: 77px;
  54. height: 77px;
  55. border: 1px solid #d9d9d9;
  56. }
  57. .weui-uploader__input-box:before, .weui-uploader__input-box:after {
  58. content: " ";
  59. position: absolute;
  60. top: 50%;
  61. left: 50%;
  62. transform: translate(-50%, -50%);
  63. background-color: #d9d9d9;
  64. }
  65. .weui-uploader__input-box:before {
  66. width: 2px;
  67. height: 39.5px;
  68. }
  69. .weui-uploader__input-box:after {
  70. width: 39.5px;
  71. height: 2px;
  72. }
  73. .weui-uploader__input {
  74. position: absolute;
  75. z-index: 1;
  76. top: 0;
  77. left: 0;
  78. width: 100%;
  79. height: 100%;
  80. opacity: 0;
  81. }
  82. .hideTrue {
  83. display: none
  84. }

js

  1. var app = getApp();
  2. Page({
  3. data: {
  4. pics: [],
  5. count: [1, 2, 3, 4, 5, 6, 7, 8, 9],
  6. isShow: true
  7. },
  8. onLoad: function (options) {
  9. // 生命周期函数--监听页面加载
  10. isShow: (options.isShow == "true" ? true : false)
  11. },
  12. // 图片上传
  13. chooseImage: function () {
  14. var _this = this,
  15. pics = this.data.pics;
  16. wx.chooseImage({
  17. count: 9 - pics.length, // 最多可以选择的图片张数,默认9
  18. sizeType: ['original', 'compressed'], // original 原图,compressed 压缩图,默认二者都有
  19. sourceType: ['album', 'camera'], // album 从相册选图,camera 使用相机,默认二者都有
  20. success: function (res) {
  21. // success
  22. var imgSrc = res.tempFilePaths; //图片路径
  23. pics = pics.concat(imgSrc); //选取的图片的地址数组
  24. // 控制触发添加图片的最多时隐藏
  25. if (pics.length >= 9) {
  26. _this.setData({
  27. isShow: (!_this.data.isShow)
  28. })
  29. } else {
  30. _this.setData({
  31. isShow: (_this.data.isShow)
  32. })
  33. }
  34. _this.setData({
  35. pics: pics
  36. })
  37. },
  38. fail: function () {
  39. // fail
  40. },
  41. complete: function () {
  42. // complete
  43. }
  44. })
  45. },
  46. // 图片预览
  47. previewImage: function (e) {
  48. var current = e.target.dataset.src
  49. wx.previewImage({
  50. current: current,
  51. urls: this.data.pics
  52. })
  53. }
  54. // 删除图片
  55. deleteImg: function (e) {
  56. var imgs = this.data.imgs;
  57. var index = e.currentTarget.dataset.index;
  58. imgs.splice(index, 1);
  59. this.setData({
  60. imgs: imgs
  61. });
  62. },
  63. uploadimg:function(){//这里触发图片上传的方法
  64. var pics=this.data.pics;
  65. app.uploadimg({
  66. url:'https://........',//这里是你图片上传的接口
  67. path:pics//这里是选取的图片的地址数组
  68. });
  69. },
  70. })

在app.js中写一个多张图片上传的方法,后面引入,你也可以写在一个JS文件中,后面引入:

  1. //多张图片上传
  2. function uploadimg(data){
  3. var that=this,
  4. i=data.i?data.i:0,//当前上传的哪张图片
  5. success=data.success?data.success:0,//上传成功的个数
  6. fail=data.fail?data.fail:0;//上传失败的个数
  7. wx.uploadFile({
  8. url: data.url,
  9. filePath: data.path[i],
  10. name: 'file',//这里根据自己的实际情况改
  11. formData:null,//这里是上传图片时一起上传的数据
  12. success: (resp) => {
  13. success++;//图片上传成功,图片上传成功的变量+1
  14. console.log(resp)
  15. console.log(i);
  16. //这里可能有BUG,失败也会执行这里,所以这里应该是后台返回过来的状态码为成功时,这里的success才+1
  17. },
  18. fail: (res) => {
  19. fail++;//图片上传失败,图片上传失败的变量+1
  20. console.log('fail:'+i+"fail:"+fail);
  21. },
  22. complete: () => {
  23. console.log(i);
  24. i++;//这个图片执行完上传后,开始上传下一张
  25. if(i==data.path.length){ //当图片传完时,停止调用
  26. console.log('执行完毕');
  27. console.log('成功:'+success+" 失败:"+fail);
  28. }else{//若图片还没有传完,则继续调用函数
  29. console.log(i);
  30. data.i=i;
  31. data.success=success;
  32. data.fail=fail;
  33. that.uploadimg(data);
  34. }
  35. }
  36. });
  37. }

效果图,可点击放大预览

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/556513
推荐阅读
相关标签
  

闽ICP备14008679号