uni.chooseImage 选择上传多张图片

// 点击单张图片上传 显示到页面
onUploads() {
	var that = this;
	uni.chooseImage({
		count: 9,
		sizeType: ['original', 'compressed'],
		sourceType: ['camera','album'], 
		success: (res) => {
			const tempFilePaths = res.tempFilePaths;
			console.log(tempFilePaths,'图片路径地址');
			const serverUrl = '域名'
			uni.uploadFile({
				url: serverUrl + '/api/index/upload',
				filePath: tempFilePaths,
				name: 'image',
				// header: {
				// 	'token': this.token,
				// },
				success: (resupload) => {
					const result = JSON.parse(resupload.data);
					console.log(result,'图片');
					if (result.code == 1) {
						this.fileList = this.fileList.concat(result.data);
						if(this.fileList.length >= 9){
							this.Uploadshow = false
						} else {
							this.Uploadshow = true
						}
					}
					
				},
			})
		}
	});
},
// 点击多张图片上传 显示到页面
	async onUpload() {
		if(this.fileList.length >= 5){
			uni.showToast({
				title:'最多上传五张',
				icon:'none'
			})
		} else {
			var that = this;
			var newArrs = [];
			var serverUrl = '域名'
			var token = uni.getStorageSync('token');
			let [err, chooseImageRes] = await uni.chooseImage({
				count: 9
			});
			
			var chooeseArr = chooseImageRes.tempFilePaths;
			var token = uni.getStorageSync('token');
			
			var promiseimg = Promise.all(chooeseArr.map((chooeseArr, index) => {
				 return new Promise(function(resolve, reject) {
					uni.uploadFile({
						url: serverUrl + '/api/index/upload',
						filePath: chooeseArr,
						name: 'image',
						header: {
							'token': token,
							// "Content-Type": "multipart/form-data",
						},
						success: (uploadFileRes) => {
							resolve(JSON.parse(uploadFileRes.data));
						},
						fail: function(err) {
							reject(err);
						}
					})
				})
			}));
						
			promiseimg.then(results => {
				for(var i in results) {
					newArrs.push(results[i].data);
				}
				
				if(that.fileList.concat(newArrs).length){
					var fileList = that.fileList.concat(newArrs);
					that.fileList = fileList.slice(0,5);
				}
				
			}).catch(errs => {
				console.log(errs);
			});
		}
	},
	

在这里插入图片描述
在这里插入图片描述
官址: https://uniapp.dcloud.io/api/media/image?id=chooseimage
在这里插入图片描述

ios浏览器可以选择多张上传图片,android机 不行。。。。。。。。。。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值