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

 

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章