uni-app 文件上传oss

	changeUserPhoto() {
		let that = this;
			uni.chooseImage({
				count: 1,
				sizetype: ['compressed'],
				success(res) {
					var imgFilesObj = res.tempFiles[0];

					that.userPhoto = imgFilesObj.path;
					uni.showLoading({
						title: '上传中',
						mask: true
					});
					// 做成一个上传对象
					var uper = uni.uploadFile({
						// 需要上传的地址
						url: 文件上传地址,
						// filePath  需要上传的文件
						filePath: imgFilesObj.path,
						name: 'file',
						formData: {
							dirName: 'cfjfb'
						},
						success(resImg) {
							let oGetTime = new Date().getTime()
									let ossObj = JSON.parse(resImg.data)
									let ossObjs = JSON.parse(ossObj);
							uni.uploadFile({
								url: ossObjs.host, //仅为示例,非真实的接口地址
								filePath: imgFilesObj.path,
								name: 'file',
								formData: {
									OSSAccessKeyId: ossObjs.OSSAccessKeyId,
									accessKey: ossObjs.accessKey,
									bucket: ossObjs.bucket,
									expire: ossObjs.expire,
									host: ossObjs.host,
									key: ossObjs.key + '/' + oGetTime,
									policy: ossObjs.policy,
									signature: ossObjs.signature,
									type: res.tempFiles[0].type,
									name: res.tempFiles[0].name,
									lastModifiedDate: res.tempFiles[0].lastModifiedDate,
									size: res.tempFiles[0].size
								},
								success: (uploadFileRes) => {
									that.userPhotoArr = [];
									that.userPhotoArr.push({
										url: ossObjs.key+'/'+oGetTime,
										sId: oGetTime,
										name: imgFilesObj.name
									});
									updateUserInfoApi({
										photo: JSON.stringify(that.userPhotoArr)
									}).then(user=>{
										uni.setStorageSync('userInfo', user.data)
									});
								}
							});
						}
					});

				}
			});
	},

猜你喜欢

转载自blog.csdn.net/Cris_are/article/details/107252081