uniapp--上传图片

uniapp–上传图片

由于uniapp自带的上传图片组件,删除图片和预览原来的图片存在bug,所以本人就在原来的组件的基础上进行了改进

实现思路

  • 父组件将已经存在的图片路径传递给子组件
    • 子组件接收到之后,imageList子组件中一个存储图片路径的数组
    • this.imageList.push(val); this.uploadImguRL += val+','; 在这两句代码的外面是一个forEach循环
    • uploadImguRL 上传到服务器的图片路径

代码

	<view>
		<!-- 公司认证 -->
		<!-- filePath 上传图片至服务器,服务器返回的上传的图片的路径 -->
		<!-- oldFilePath 已经上传过的图片的路径   filecount上传的图片的数量-->
		<uploadImg @filePath="filePath" :oldFilePath="oldFilePath" :filecount="componyCount"></uploadImg>
		<view class="submit-btn">
			<button type="primary" @tap="confirm">确定</button>
		</view>
	</view>
</template>
<script>
	// 上传图片的组件
	import uploadImg from '../upload-file/upload.vue'
	import api from '../../../common/api.js'
	import commonData from '../../../common/common-data.js'
	export default {
		data(){
			return {
				// 存储已经存在的图片的路径
				// oldFilePath是显示之前用户信息的
				oldFilePath:[],
				// 存储上传之后服务器返回的路径
				uploadImgUrl:'',
				componyCount:{compony:10}
			}
		},
		components:{
			uploadImg
		},
		methods:{
			confirm(){
				// 点击确定,调用上传图片的接口
				// console.log(this.uploadImgUrl);
				// 防止用户直接点击确定
				let that = this;
				// 选择图片之后,服务器返回的图片路径比较慢
				if(that.uploadImgUrl == ''){
					uni.request({
						url: api.updateAddress,
						method: 'GET',
						header:{
							"token":commonData.getUserinfoThree().userinfo.token
						},
						data: {
							other_image:that.uploadImgUrl,
						},
						success: res => {
							uni.showToast({
								title: res.data.msg
							});
						},
						fail: () => {
							uni.showToast({
								title: '上传失败'
							});
						}
					});
				} else {
					uni.request({
						url: api.updateAddress,
						method: 'GET',
						header:{
							"token
uni-file-picker是uni-app框架中的一个组件,用于在移动端应用中实现图片上传功能。通过该组件,用户可以选择图片并将其上传到服务器。 以下是使用uni-file-picker组件实现图片上传的步骤: 1. 在页面中引入uni-file-picker组件,并设置相关属性: ```html <template> <view> <uni-file-picker limit="1" ref="files" :auto-upload="false" @select="select" @delete="del" v-model="one"></uni-file-picker> <button @click="upload">上传图片</button> </view> </template> ``` 其中,limit属性用于限制选择图片的数量,ref属性用于获取组件实例,auto-upload属性设置为false表示不自动上传图片,@select和@delete分别是选择图片和删除图片时触发的事件,v-model用于双向绑定选择的图片。 2. 在页面的script标签中定义相关方法: ```javascript <script> export default { data() { return { one: [] // 存储选择的图片 } }, methods: { select(files) { // 选择图片时触发的方法 this.one = files }, del(files) { // 删除图片时触发的方法 this.one = files }, upload() { // 上传图片的方法 // 可以在这里调用接口将图片上传到服务器 console.log(this.one) } } } </script> ``` 在select方法中,将选择的图片赋值给one变量;在del方法中,将删除图片后的结果赋值给one变量;在upload方法中,可以调用接口将选择的图片上传到服务器。 通过以上步骤,就可以在uni-app中使用uni-file-picker组件实现图片上传功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值