uniapp开发小程序之上传图片(拍照或从相册中取)

<view @tap="ChooseImage">
	上传图片
</view>
methods:{
	ChooseImage() {
		uni.chooseImage({
			count: 1, //可选择数量,默认9
			sizeType: ['compressed'], //只能上传压缩图
			sourceType: ['album','camera'], //从相册选择或从使用相机
			success: async (res) => {
				if(res.tempFiles[0].size > 10 * 1024 * 1000){  //上传图片大小限制
					uni.showToast({
						title: "照片大小不能10MB", 
						icon: "none"
					})
					return
				}
				let url = res.tempFilePaths[0];
				try{
					uni.showLoading({
						title:"上传中...",
						mask:true
					})
					let data = await this.$upLoadImg({
					   apiName:"uploadHouse",
					   filePath:res.tempFilePaths[0],
					   name:"file",
					})
					this.imgUrl = data.data.url;
				}catch(e){
					console.log(e);
				}finally{
					uni.hideLoading();
				}
			}
		}); 
	},
}
 

猜你喜欢

转载自blog.csdn.net/weixin_36185028/article/details/104920650
今日推荐