小程序多张图片上传;主要运用promise的all,代码如下:
本文中的basicURL 是我在app.js中配置的全局变量,即请求接口的域名地址;
data中的数据:
imageList:[],
1、选择图片
微信小程序自带chooseImage方法。选择图片,把图片路径保存到imageList数组中;
chooseImage: function () {
var _this = this;
wx.chooseImage({
sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success: function (res) {
// 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
console.log(res);
var tempFilePaths = res.tempFilePaths;
var imageList = _this.data.imageList;
for (var i = 0; i < tempFilePaths.length; i++) {
if (imageList.length >= 9) {
_this.setData({
imageList: imag