uniapp同步上传图片

uniapp(uview)上传图片

单张:

let _this = this
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['compressed'],
					sourceType: ['album'],
					success: res => {
						let tiemr = new Date();
						let address = tiemr.getFullYear() + '' + (tiemr.getMonth() + 1) + '' + tiemr.getDate();
						address = 'happySingle/' + address + '/';
						var imageSrc = res.tempFilePaths[0];
						let str = res.tempFilePaths[0].substr(res.tempFilePaths[0].lastIndexOf('.'));
						let nameStr = address + tiemr.getTime() + str;
						let ossUrl = _this.aliOss.host + '/';
						uni.uploadFile({
							url: ossUrl, //输入你的bucketname.endpoint
							filePath: imageSrc,
							fileType: 'image',
							name: 'file',
							formData: {
								name: nameStr,
								key: nameStr,
								policy: _this.aliOss.policy, // 输入你获取的的policy
								OSSAccessKeyId: _this.aliOss.accessid, // 输入你的AccessKeyId
								success_action_status: '200', // 让服务端返回200,不然,默认会返回204
								signature: _this.aliOss.signature // 输入你获取的的signature
							},
							success: res => {
								if (res.statusCode == '200') {
									_this.formData.topImg = ossUrl + nameStr
								} else {
									uni.showToast({
										title: '上传图片失败',
										duration: 2000
									});
								}
							}
						});
					}
				});

多张:

let _this = this
				_this.fileList5 = event.file
				console.log(_this.fileList5, '_this.fileList5')
				for (let i = 0; i < _this.fileList5.length; i++) {
					let ossUrl = _this.aliOss.host + '/';
					let tiemr = new Date();
					let address = tiemr.getFullYear() + '' + (tiemr.getMonth() + 1) + '' + tiemr.getDate();
					address = 'happySingle/' + address + '/';
					var imageSrc = _this.fileList5[i].url
					let str = _this.fileList5[i].url.substr(_this.fileList5[i].url.lastIndexOf('.'));
					let nameStr = address + tiemr.getTime() + str;
					console.log(imageSrc, 'imageSrc')
					uni.uploadFile({
						url: ossUrl, //输入你的bucketname.endpoint
						filePath: imageSrc,
						fileType: 'image',
						name: 'file',
						formData: {
							name: nameStr,
							key: nameStr,
							policy: _this.aliOss.policy, // 输入你获取的的policy
							OSSAccessKeyId: _this.aliOss.accessid, // 输入你的AccessKeyId
							success_action_status: '200', // 让服务端返回200,不然,默认会返回204
							signature: _this.aliOss.signature // 输入你获取的的signature
						},
						success: res => {
							if (res.statusCode == '200') {
								console.log(ossUrl + nameStr)
								// _this.imgArr.push(ossUrl + nameStr)  异步
                                _this.imgArr[i] = ossUrl + nameStr   同步
								_this.formData.showImg = _this.imgArr.join(',')
							} else {
								uni.showToast({
									title: '上传图片失败',
									duration: 2000
								});
							}
						}
					});
				}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值