微信小程序实现上传多张图片、删除图片

这篇文章主要为大家详细介绍了微信小程序实现上传多张图片、删除图片,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

最近在做微信小程序,遇到上传多张图片到服务器,计算上传图片的张数,并且可以手动删除图片,下面是效果图

效果图:

本来用的是小程序提供的 mp-uploader 上传图片的组件,无奈次组件删除效果不是我想要的,只能用 wx.chooseImage进行上传图片,在使用uplaodFile将图片发送给后台服务器。

下面直接展示代码:

wxml:

   患者病历{{imgShow.length}}/6    

wxss:

 /* 上传图片 */ .images-boxc { position: relative; border: dashed 1px #bfbfbf; width: 139rpx; height: 139rpx; margin-right: 32rpx; margin-bottom: 32rpx; } .delete-image { position: absolute; width: 30rpx; height: 30rpx; right: 16rpx; top: 16rpx; } .add-image { display: flex; flex-wrap: wrap; } .image_size { width: 139rpx; height: 139rpx; } .image_sizen { height: 142rpx; }

js:

 data: { count: 6, //设置最多6张图片 allImg: [], imgShow: [], }, // 上传图片 chooseImage: function() { wx.showLoading({ title: '加载中...', mask: true }) var that = this; var imgShow = that.data.imgShow; var count = that.data.count - imgShow.length; //设置最多6张图片 wx.chooseImage({ count: count, sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 success: function(res) { console.log(res) that.uplaodFile(res) for (var i = 0, h = res.tempFilePaths.length; i  { util.uploadFile('/fastdfsServer/fileUpload', element, 'file', {}, function(res) { //上传本地图片地址到服务器 返回地址 存放到input提交时取值 res = JSON.parse(res); if (res.responseCode == 0) { sysMsg.sysMsg("上传成功", 1000, 'success'); that.setData({ allImg: that.data.allImg.concat(res.responseBody) }); } else { sysMsg.sysMsg("上传失败", 1500, 'error'); } }); }); // 文件上传的函数,返回一个promise return new Promise((resolve, reject) => { resolve({ urls: files.tempFilePaths }); setTimeout(() => { reject('some error') }, 10000) }) }, uploadError(e) { console.log('upload error', e.detail) }, uploadSuccess(e) { // this.setData({ // allImg: this.data.allImg.concat(e.detail.urls[0]) // }); console.log('upload success', e.detail, e.detail.urls) },

为大家推荐现在关注度比较高的微信小程序教程一篇:《微信小程序开发教程》小编为大家精心整理的,希望喜欢。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持html中文网。

以上就是微信小程序实现上传多张图片、删除图片的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » JavaScript 教程