uni-app 小程序多图上传

uni-app 小程序多图上传:

官方提示说,App支持多文件上传,微信小程序只支持单文件上传,传多个文件需要反复调用本API。所以跨端的写法就是循环调用本API

步骤:

//1.首先通过 uni.chooseImage接口获取到一个本地资源的临时文件路径
// this.uploadFiles  => 图片临时路径集合
uni.chooseImage({
	count: 9, //默认9
	sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
	sourceType: ['album'], //从相册选择
	success: res => {
		for (let i = 0; i < res.tempFilePaths.length; i++) {
			this.uploadFiles.push(res.tempFilePaths[i]);
		}
	}
});
//2.确认上传
let arr = [];
for (let i = 0; i < this.uploadFiles.length; i++) {
	let pro = uni.uploadFile({
		url: '开发者服务器 url',
		filePath: this.uploadFiles[i],
		fileType: 'image',
		name: 'file',
		header: {
			'Content-Type': 'multipart/form-data',
			token: uni.getStorageSync('token')
		}
	});
	arr.push(pro);
}
Promise.all(arr).then(result => {
	let pic = '';
	result.forEach(item => {
		pic += JSON.parse(item[1].data).msg + ';';
	});
	console.log(pic)
})
//3.删除图片
DelImg(e) {
	uni.showModal({
		title: '提示',
		content: '确定要删除这张照片吗?',
		cancelText: '再想想',
		confirmText: '再见',
		success: res => {
			if (res.confirm) {
				this.uploadFiles.splice(e.currentTarget.dataset.index, 1);
			}
		}
	});
}
//4.图片预览
ViewImage(e) {
	uni.previewImage({
		urls: this.uploadFiles,
		current: e.currentTarget.dataset.url
	});
},
//5.想起啥再写吧

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值