当前位置: 代码网 > it编程>编程语言>Javascript > uni-file-picker文件选择上传功能实现代码

uni-file-picker文件选择上传功能实现代码

2024年10月28日 Javascript 我要评论
一、 基础用法uni-file-picker文档说明mode="grid" ,可以使用九宫格样式选择图片limit="1" ,则最多选择张图片file-medi

一、 基础用法

uni-file-picker文档说明

mode="grid" ,可以使用九宫格样式选择图片

limit="1" ,则最多选择张图片

file-mediatype="image",限定只选择图片

file-extname='png,jpg',限定只选择 png和jpg后缀的图片

auto-upload="false",可以停止自动上传,通过ref调用upload方法自行选择上传时机。与ref="files"搭配使用,this.$refs.files.upload()

<uni-file-picker 
    v-model="imagevalue"  
    file-mediatype="image"
    mode="grid"
    file-extname="png,jpg"
    :limit="1"
    @progress="progress" 
    @success="success" 
    @fail="fail" 
    @select="select"
/>
<script>
    export default {
        data() {
            return {
                imagevalue:[]
            }
        },
        methods:{
            // 获取上传状态
            select(e){
                console.log('选择文件:',e)
            },
            // 获取上传进度
            progress(e){
                console.log('上传进度:',e)
            },
            // 上传成功
            success(e){
                console.log('上传成功')
            },
            // 上传失败
            fail(e){
                console.log('上传失败:',e)
            }
        }
    }
</script>

二、案例一(只上传一张图片)

<template>
  <view class="container example">
    <uni-forms ref="baseform" :modelvalue="baseformdata" label-position="top">
      <uni-forms-item label="图片上传">
       <uni-file-picker 
        v-model="imagevalue" 
        filemediatype="image" 
        mode="grid" 
        @select="select"   
        limit="1"
        ></uni-file-picker>
      </uni-forms-item>
    </uni-forms>
  </view>
</template>
export default {
    data() {
      return {
        //图片
        imagevalue:[],
      }
    },
    methods:{
      //图片上传
      select(e){
        const tempfilepaths = e.tempfilepaths;
        //获取图片临时路径
        const imgurl=tempfilepaths[0]
        uni.uploadfile({
          //图片上传地址
          url: 'https://xxx.xxx.com/api/uploadpic/', 
          filepath: imgurl,
          //上传名字,注意与后台接收的参数名一致
          name: 'imgurl',
          //设置请求头
          header:{"content-type": "multipart/form-data"},
          //请求成功,后台返回自己服务器上的图片地址
          success: (uploadfileres) => {
            console.log('uploadfileres',json.parse(uploadfileres.data));   
            //处理数据
            const path=json.parse(uploadfileres.data)
            //赋值,前端渲染
            this.baseformdata.photo=path.imgurl
          }
        });
      }, 
    }  
  }

三、案例二(上传多张图片)

<template>
  <view class="container example">
    <uni-forms ref="baseform" :modelvalue="baseformdata" :rules="rules" label-position="top">
      <uni-forms-item label="图片上传">
        <uni-file-picker
         v-model="imagevalue"
         filemediatype="image" 
         mode="grid" 
         @select="select"   
         @delete="delimg"
         limit="9"></uni-file-picker>
      </uni-forms-item>
    </uni-forms>
  </view>
</template>
<script>
  export default {
    data() {
      return {
        imgurl:'',
        //图片
        imagevalue:[],
      };
    },
    methods:{
      //图片上传
      select(e){
        const tempfilepaths = e.tempfilepaths;
        const imgurl=tempfilepaths[0]
        uni.uploadfile({
          url: 'https://xxx.xxx.com/api/uploadpic2/', 
          filepath: imgurl,
          name: 'imgurl',
          header:{"content-type": "multipart/form-data"},
          success: (uploadfileres) => {
            console.log('uploadfileres',json.parse(uploadfileres.data));
            let path=json.parse(uploadfileres.data)
            //后端返回的地址,存入图片地址
            this.imagevalue.push({
              url:this.imgurl+path.imgurl,
              name:''
            })
            console.log('imagevalue',this.imagevalue)
          }
        });
      },
      //图片删除
      delimg(e){
        console.log('456',e)
        const num = this.imagevalue.findindex(v => v.url === e.tempfilepath);
        this.imagevalue.splice(num, 1);
      }
    },
    onload() { 
      //全局定义的图片访问地址前缀
      this.imgurl=this.$imgurl
    }
  }
</script>

到此这篇关于uni-file-picker文件选择上传功能实现的文章就介绍到这了,更多相关uni-file-picker文件上传内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com