uni-app实现图片上传功能

效果

uni-app实现图片上传功能_第1张图片

代码


			
			
		

 

			async handleSelect(res) {
				await this.uploadImg(res.tempFiles[0],res.tempFilePaths, 1);
			},
			async uploadImg(tempFiles,tempFilePaths, token) {
				if (!tempFilePaths.length) return;  //如果没有选择图片就退出
				uni.uploadFile({
							url:`${imgUrl}/api/app/file-address/upload-imm-file`,//后台地址
							// #ifdef H5
														file:tempFiles.file,
														// #endif
														// #ifdef APP-PLUS
														filePath:tempFilePaths[0],
														name:'file',
														// #endif
							success: (uploadFileRes) => {
								this.formData.ViolationImg=`/Files/${JSON.parse(uploadFileRes.data).relativeNetPath}`
								this.$refs.form.setValue('ViolationImg',this.formData.ViolationImg)
							}
						});
			},
			handleDelete(err) { // 删除图片
				this.filePathsList.length=0
				this.formData.ViolationImg=''
				this.$refs.form.validateField('ViolationImg').then((res)=>{	
					
				}).catch((err)=>{
					
				})
			},

你可能感兴趣的:(uni-app)