uniapp点击上传图片功能

uniapp点击上传图片功能

2020/07/28

// 点击图片上传
clickUpload() {
	let _self = this;
	uni.chooseImage({
		count: 1,
		sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
		sourceType: ['album'], //从相册选择
		success: (chooseImageRes) => {
			// console.log(chooseImageRes);
			const tempFilePaths = chooseImageRes.tempFilePaths;
			// console.log(tempFilePaths[0]);
			_self.IMGS = tempFilePaths[0];
			uni.showLoading({
				title:'正在上传'
			});
			const uploadTask = uni.uploadFile({
				url: 'api/index/upload',
				filePath: tempFilePaths[0],
				name: 'file',
				formData: {
					"user":'test'
				},
				success: (res) => {
					console.log((JSON.parse(res.data)).data);
					uni.hideLoading();
					uni.showToast({
						title:'上传成功',
						icon:'none',
						duration:2000
					});
					uni.setStorageSync('yuzhe_papers_image', (JSON.parse(res.data)).data);
				},
				fail: (error) => {
					console.log('失败原因',error);
				}
			});
		}
	});
},

你可能感兴趣的:(uniapp,javascript,前端)