微信小程序实现多张照片上传功能
微信小程序实现多张照片上传
1.功能实现
当选择图片后,生成对象tempFilePaths文件路径。在通过for循环依次的图片的src上传到服务器。当服务器的状态码为200且图片上传完毕后将图片的src转化为Json字符串存在数组中以便将其添加到数据库
2.代码实现
1.mp-uploader
<view class="page">
<view class="page__bd">
<mp-cells>
<mp-cell>
<mp-uploader select="{{selectFile}}" upload="{{uplaodFile}}" files="{{files}}" max-count="4" title="附件上传" tips="最多可上传4张照片"></mp-uploader>
</mp-cell>
</mp-cells>
</view>
</view>//data中
this.setData({
selectFile: this.selectFile.bind(this),
uplaodFile: this.uplaodFile.bind(this)
}) uplaodFile(files) {
console.log('upload files', files)
console.log('upload files', files)
// 文件上传的函数,返回一个promise
return new Promise((resolve, reject) => {
const tempFilePaths = files.tempFilePaths;
//上传返回值
const that = this;
const object = {};
for (var i = 0; i < tempFilePaths.length; i++) {
let filePath = tempFilePaths[i]
wx.uploadFile({
filePath: filePath,
name: 'file',
url: 'http://localhost:3000/upload/upload',
success: function(res){
console.log('444',res.statusCode)
if (res.statusCode=== 200 ) {
const url = JSON.parse(res.data).url
that.data.files.push(url)
if (that.data.files.length === tempFilePaths.length) {
object.urls = that.data.files;
resolve(object) //这就是判断是不是最后一张已经上传了,用来返回,
}
} else {
reject('error')
}
}
})
}
})
// 文件上传的函数,返回一个promise
},

2.chooseImage
<view>
<block wx:for="{{images}}" wx:for-item="src">
<image src="{{src}}"></image>
</block>
<view bindtap="upload">上传</view>upload(){
let that = this;
wx.chooseImage({//异步方法
count: 9,//最多选择图片数量
sizeType: ['original', 'compressed'],//选择的图片尺寸 原图,压缩图
sourceType: ['album', 'camera'],//相册选图,相机拍照
success(res) {
//tempFilePaths可以作为图片标签src属性
const tempFilePaths = res.tempFilePaths
console.log("选择成功", res)
for (let i = 0; i < tempFilePaths.length; i++) {//多个图片的循环上传
wx.cloud.uploadFile({//上传至微信云存储
cloudPath: 'myImage/' + new Date().getTime() + "_" + Math.floor(Math.random() * 1000) + ".jpg",//使用时间戳加随机数作为上传至云端的图片名称
filePath: tempFilePaths[i],// 本地文件路径
success: res => {
// 返回文件 ID
console.log("上传成功", res.fileID)
that.setData({
images: res.fileID//获取上传云端的图片在页面上显示
})
wx.showToast({
title: '上传成功',
})
}
})
}
}
})
}3.页面展示
图片的src在数据库中是以字符串的形式存储。当需要展示时我们只需要将字符串转化为数组对象即可
原始数据 “http://localhost:3000/images/17112466754606371.jpg”,“http://localhost:3000/images/17112466755133666.jpg”,“http://localhost:3000/images/17112466756494564.jpg”]
getShare().then(res=>{
const list=res.data
list.forEach(obj => {
console.log('8888',obj.img)
const imgString = obj.img;
const trimmedString = imgString.replace('["', '').replace('\"]', '');
const imgArray = trimmedString.split('"\,\"');
console.log('444',imgArray)
obj.img = imgArray;
})
this.setData({
shareList:list
})
})
🎉写在最后
🍻伙伴们,如果你已经看到了这里,觉得这篇文章有帮助到你的话不妨点赞👍或 Star ✨支持一下哦!手动码字,如有错误,欢迎在评论区指正💬~
到此这篇关于微信小程序实现多张照片上传的文章就介绍到这了,更多相关小程序多张照片上传内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
javascript removeChild 导致的内存泄漏
最近看到司徒正美的一篇文章《移除DOM节点》,文中说到在IE中移除容器类节点,会引起内存泄露。2010-08-08
JavaScript前端中的伪类元素before和after使用详解
before和after也算是css里面最常见的元素了,而我却一直不太了解,再不学一下就真的太不像话了。所以学习一下,需要的朋友们下面随着小编来一起学习吧2023-02-02
使用contextMenu插件实现Bootstrap table弹出右键菜单
如今Bootstrap这个前端框架已被许多人接受并应用在不同的项目中,其中“开发高效,设备兼容”的特点表现得非常明显。这篇文章主要介绍了使用contextMenu插件实现Bootstrap table弹出右键菜单,需要的朋友可以参考下2017-02-02


最新评论