uniapp框架开发上传图片

<view class="solids" @tap="ChooseImage(1)" >
	<image mode="scaleToFill" :src="cardAimg">image>
	<view class="title1">
		身份证正面
	view>
view>
//选择图片
ChooseImage: async function(type) {
	    // #ifdef APP-PLUS
	    // #endif
	    uni.chooseImage({//从本地相册选择图片或使用相机拍照API
		    sourceType: ['album','camera'],
			sizeType: ['compressed'], 
	        count: 1,
	        success: (res) => {
				console.log(res)
				var that=this;
				var tempFilePaths = res.tempFilePaths[0];
				console.log(tempFilePaths)
				uni.uploadFile({//uniapp上传API
					url: config.requestUrl+'/uploadCt/uploadImg',
					filePath: tempFilePaths,
					name: 'file',
					method:'POST',
					header:{
						'Content-Type': 'application/x-www-form-urlencoded',
					},
					success: (res) => {
						let imgSrc= res.data.split(',')[2].split('"')[5]

						console.log(imgSrc)
						if(type==1){
							that.cardAimg=imgSrc;
							console.log(that.cardAimg);
						}else if(type==2){
							that.cardBimg=imgSrc;
							console.log(that.cardBimg);
						}else if(type==3){
							that.cardCimg=imgSrc;
						}else if(type==4){
							that.cardDimg=imgSrc;
						}
					},
					fail:(err) => {
						console.log(err)
					}
				})
	        },
	        fail: (err) => {
	            // #ifdef APP-PLUS
				console.log(err)
	            if (err['code'] && err.code !== 0 && this.sourceTypeIndex === 2) {
	                this.checkPermission(err.code);
	            }
	            // #endif
	        }
	    })
	},
	async checkPermission(code) {
	    let type = code ? code - 1 : this.sourceTypeIndex;
	    let status = permision.isIOS ? await permision.requestIOS(sourceType[type][0]) :
	        await permision.requestAndroid(type === 0 ? 'android.permission.CAMERA' :
	            'android.permission.READ_EXTERNAL_STORAGE');
	
	    if (status === null || status === 1) {
	        status = 1;
	    } else {
	        uni.showModal({
	            content: "没有开启权限",
	            confirmText: "设置",
	            success: function(res) {
	                if (res.confirm) {
	                    permision.gotoAppSetting();
	                }
	            }
	        })
	    }
	
	    return status;
	},

文档: https://uniapp.dcloud.io/api/media/image

你可能感兴趣的:(uniapp)