uniapp上传图片 (uni-file-picker)

使用的是uniapp官方文档扩展组件

点击跳转

下面代码中详细参数请点击官方文档查看

<template>
	<view>
		
			<uni-file-picker
				v-model="imageValue" 
				fileMediatype="image" 
				mode="grid"
				limit="1"
				@select="select"
				@progress="progress" 
				@success="success" 
				@fail="fail" 
			></uni-file-picker>
		
		<view class="btn" @click="upData">
			立即提交
		</view>
	</view>
</template>
<script>
	import {UploadImg,UpDataImg} from '@/api/order.js'
	export default {
		data() {
			return {
				imageValue:[],
				file:'',
				isLogin:'',
				resquestUrl:'',
				imgLuJing:''
			}
		},
		onLoad() {
		},
		methods:{
			uploadFile() {
			 uni.showLoading({
			 	title: '上传中'
			 });
			 
			  const formData = new FormData(); // 创建一个 FormData 对象
			  formData.append('file', this.file); // 将文件添加到 FormData 中
			 
			  var uploadTask = uni.uploadFile({
			    url: 'http:106.xx.xx.xx/product/updateProductImage', // 接口地址
			    filePath: this.file.path, // 文件路径(可选)
			    name: 'image', // 后台服务器接收文件的字段名
			    // formData: formData, // 传递的额外数据
			    success: (res) => {
					uni.hideLoading();
					uni.showToast({
						title: '上传成功',
						duration: 1000,
						icon:'none'
					});
				  this.imgLuJing = JSON.parse(res.data)
			
			    },
			    fail: (err) => {
					uni.showToast({
						title: '上传失败,请重试',
						duration: 1000,
						icon:'none'
					});
			      // console.error(err,"上传失败");
			    },
			  });
			},
			upData(){
				//调接口上传图片
			},
		
			// 获取上传状态
			select(e){
				this.file = e.tempFiles[0].file
				// console.log('选择文件:',this.file)
				this.uploadFile()
			},
			// 获取上传进度
			progress(e){
				console.log('上传进度:',e)
			},
			
			
		}
	}
</script>
<style lang="scss">
	.btn{
		width: 208px;
		height: 40px;
		margin: 0px auto;
		background: linear-gradient(180deg, #00A5FE 0%, #007BFD 100%);
		border-radius: 49px 49px 49px 49px;
		opacity: 1;
		margin-top: 100px;
		color: #fff;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
	}
</style>


 

上传图片接口实例

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值