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)
				}
			});

		}
	})
},

猜你喜欢

转载自blog.csdn.net/qq_38881495/article/details/130678142