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>


 

上传图片接口实例

在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uniappuni-file-picker组件可以实现在真机上上传图片的功能。你可以按照以下步骤来实现: 1. 在uniapp项目中引入uni-file-picker组件。可以通过npm安装或者直接下载组件文件。 2. 在需要使用图片上传功能的页面中,使用uni-file-picker组件,并设置相应的属性和事件。 3. 在uni-file-picker组件的属性中,设置上传的接口地址、上传的文件类型、最大上传数量等。 4. 在uni-file-picker组件的事件中,监听上传成功和上传失败的回调函数。 5. 在上传成功的回调函数中,可以处理上传成功后的逻辑,比如将上传成功的图片显示在页面上。 6. 在上传失败的回调函数中,可以处理上传失败后的逻辑,比如提示用户上传失败的信息。 以下是一个示例代码: ```html <template> <view> <uni-file-picker :url="uploadUrl" :file-type="fileType" :max-count="maxCount" @success="uploadSuccess" @fail="uploadFail" ></uni-file-picker> </view> </template> <script> export default { data() { return { uploadUrl: 'http://your-upload-api-url', // 上传接口地址 fileType: 'image', // 上传文件类型 maxCount: 5 // 最大上传数量 } }, methods: { uploadSuccess(res) { // 上传成功的回调函数 console.log('上传成功', res) // 处理上传成功后的逻辑,比如将上传成功的图片显示在页面上 }, uploadFail(res) { // 上传失败的回调函数 console.log('上传失败', res) // 处理上传失败后的逻辑,比如提示用户上传失败的信息 } } } </script> ``` 请注意,以上代码仅为示例,具体的接口地址、文件类型和最大上传数量需要根据实际情况进行设置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值