uniapp使用七牛云上传demo效果(整理)

七牛云上传地区

 uploadURL = 'https://up.qiniup.com';      //代表华东区域
 uploadURL = 'https://up-z1.qiniup.com';   //代表华北区域
 uploadURL = 'https://up-z2.qiniup.com';   //代表华南区域
 uploadURL = 'https://up-as0.qiniup.com';  //代表东南亚区域
 uploadURL = 'https://up-na0.qiniup.com';  //代表北美区域
addPic() {
	uni.chooseImage({
		count: 1,
		sizeType: ["original", "compressed"], //可以指定是原图还是压缩图,默认二者都有
		sourceType: ["album", "camera"], //从相册选择
		success: (res) => {
			uni.showLoading({
				title: "图片上传中...",
				mask: true,
			})
			// res.tempFilePaths.forEach((filePath, index) => {
			// 如果是选择多张, 放循环里面上传
			// })
			let img = res.tempFilePaths[0]; //拿到里面的
			this.$.ajax("POST", "/index/qiniuToken", {}, (res) => {
				if (res.code === 200) {
					let key = new Date().getTime();
					uni.uploadFile({
						url: "https://up-z1.qiniup.com", //代表华北区域
						filePath: img,
						name: 'file',
						method: "POST",
						formData: res.data,
						success: uploadFileRes => {
							//uploadFileRes 返回了data是一个json字符串 
							//拿到里面的key拼接上域名,再反出去就ok了
							let strToObj = JSON.parse(uploadFileRes.data);
							console.log(strToObj)
							// res.data.domain 后台返回的域名
							var imgUrl = res.data.domain + '/' + strToObj.key
							this.picImg.push(imgUrl); //回显的图片链接

							var form = {
								ajax_type: 'form',
								army_photo: this.picImg[0],
							}
							this.userInfoEditWay(form)
							uni.hideLoading();
						},
						fail: fail => {
							uni.showToast({
								title: "网络错误",
								icon: "none"
							});
							data.fail(fail); //反出去错误信息
							uni.hideLoading();
						}
					})
				} else {
					this.$.toast(res.message)
				}
			});

		}
	})
},
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值