vue+element upload上传带参数的实例

 更新时间:2022年04月24日 17:18:20   作者:QQ帝国  
这篇文章主要介绍了vue+element upload上传带参数的实例,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

element upload上传带参数

<el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">保存</el-button>
   <el-upload
    class="upload-demo"
    ref="upload"
    :action="action()"
    :on-preview="handlePreview"
    :before-upload="beforeUpload"  
    :data="uploadData"    
    :on-error = "error"
    :on-success="successResave"
    :on-remove="handleRemove"
    :file-list="fileList"
    :auto-upload="false">
    <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
    <!-- <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button> -->
    <!-- <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> -->
    </el-upload>

data里面参数

uploadData:null,

methods里面

action(){
       return this.portC+"uplodResultsScore"
     },
     //上传
     submitUpload() {
 
        this.$refs.upload.submit();
      },
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
 
        console.log(file);
      },
      beforeUpload (file) {     
          
         this.uploadData = {dstype:this.proType,name:this.mobanname,dsmark:this.value5,};
             console.log(this.uploadData)
            let promise = new Promise((resolve) => {
                this.$nextTick(function () {
                    resolve(true);
                });
            });
            return promise; //通过返回一个promis对象解决
     },
     //上传成功钩子
     successResave(response, file, fileList){
           console.log(response)
           if(response.code==10001){
              alert("保存成功")  
               this.proType="";
               this.mobanname="";
               this.value5="";
               this.$router.push("/moban")
           }
     },
    //失败钩子
     error(response, file, fileList){
           console.log(response)
     },
     //选择家化实效
      selectType(val){
         console.log(val)
         if(val==1){
             this.$router.push("/new")
         }else{
             this.$router.push("/new1")
         }
      },

效果图

element上传函数带参数,自定义传参

下面这是标签

可以看出来 :

http-request="(params) =>beforeMasterPictureUpload(params,‘ruleForm',fileList0)"

http-request 传递参数的方法 是可以这样写的,其他方法一样,但是如果写错少些可能会导致覆盖原来的方法。

<el-upload :class="{hidesse:hideUpload0,'avatar-uploader':true}" 
                        multiple action="这里不需要填或者随便写"
                        list-type="picture-card" :before-upload="beforeAvatarUpload"
                        :on-preview="(params) =>handlePictureCardPreview(params,'ruleForm',fileList0)"
                        :http-request="(params) =>beforeMasterPictureUpload(params,'ruleForm',fileList0)"
                        :on-change="(params,fileList) =>onChangeMaster(params,fileList,'ruleForm','fileList0')"
                        :on-exceed="OnExceed"
                        :on-remove="(params) =>handleRemove(params,'ruleForm',fileList0,'fileList0')" :limit="5"
                        :file-list="fileList0" @click="titext">
                        <i class="el-icon-plus"></i>
                    </el-upload>

下面是JS 接受方法 同样 我们拿 http-request 这个方法做例子

beforeMasterPictureUpload(params, fromData, prop) {
    console.log('----aaaa', this.fileList);
    console.log('file', params, fromData, prop);
}

大家可以输出看看结果 是否有拿到你上面传递过来的字符串或者是值

我将源码放上

HTML标签

<el-upload :class="{hidesse:hideUpload0,'avatar-uploader':true}" 
                        multiple action="这里不需要填或者随便写"
                        list-type="picture-card" :before-upload="beforeAvatarUpload"
                        :on-preview="(params) =>handlePictureCardPreview(params,'ruleForm',fileList0)"
                        :http-request="(params) =>beforeMasterPictureUpload(params,'ruleForm',fileList0)"
                        :on-change="(params,fileList) =>onChangeMaster(params,fileList,'ruleForm','fileList0')"
                        :on-exceed="OnExceed"
                        :on-remove="(params) =>handleRemove(params,'ruleForm',fileList0,'fileList0')" :limit="5"
                        :file-list="fileList0" @click="titext">
                        <i class="el-icon-plus"></i>
                    </el-upload>

JS语句

// 主图上传之前
                beforeAvatarUpload(file) {
                    console.log('主图上传之前:', file)
                    const idJPG =
                        file.type === "image/jpeg" || "image/gif" || "image/png" || "image/bmp";
                    if (!idJPG) {
                        this.$message.error(
                            '123123'
                        );
                    }
                    return idJPG;
                },
                // 图片触发
                onChangeMaster(file, fileList, fromData, prop) {
                    // fileList 当前上传框的数据 
                    switch (prop) {
                        case 'fileList0':
                            { this.hideUpload0 = fileList.length >= this.limitCount; }
                            break;
                        case 'fileList1':
                            { this.hideUpload1 = fileList.length >= this.limitCount; }
                            break;
                        case 'fileList2':
                            { this.hideUpload2 = fileList.length >= this.limitCount; }
                            break;
                        case 'fileList3':
                            { this.hideUpload3 = fileList.length >= this.limitCount; }
                            break;
                        case 'fileList4':
                            { this.hideUpload4 = fileList.length >= this.limitCount; }
                            break;
                        case 'fileList5':
                            { this.hideUpload6 = fileList.length >= this.limitCount; }
                            break;
                        default:
                            break;
                    }
                    console.log('判断一次', file, fileList, this.fileList0)
                    if (this.inde > 0) {
                        return
                    } else {
                        //let existFile = fileList.slice(0, fileList.length - 1).find(f => f.name === file.name)
                        //if (existFile) {
                           // this.inde++
                          //  console.log(existFile)
                          //  this.$message.error(
                          //      '图片重复' + existFile.name
                        //    );
                        //    // fileList.pop()
                     //   }
                        this[prop] = fileList
                    }
                },
                // 图片个数超出限制
                OnExceed(file, fileList) {
                    this.$message.error(
                        '每个选项最多上传5张', "error"
                    );
                },
                handleRemove(params, fileList, prop, item) {
                    prop.forEach((obj, index) => {
                        console.log(index, obj)
                        if (params.uid == obj.uid) {
                            prop.splice(index, 1)
                        }
                    })
                    console.log('删除后的数组', prop);
                    setTimeout(() => {
                        switch (item) {
                            case 'fileList0':
                                { this.hideUpload0 = prop.length >= this.limitCount; }
                                break;
                            case 'fileList1':
                                { this.hideUpload1 = prop.length >= this.limitCount; }
                                break;
                            case 'fileList2':
                                { this.hideUpload2 = prop.length >= this.limitCount; }
                                break;
                            case 'fileList3':
                                { this.hideUpload3 = prop.length >= this.limitCount; }
                                break;
                            case 'fileList4':
                                { this.hideUpload4 = prop.length >= this.limitCount; }
                                break;
                            case 'fileList5':
                                { this.hideUpload6 = prop.length >= this.limitCount; }
                                break;
                            default:
                                break;
                        }
                        console.log(prop.length >= this.limitCount)
                    }, 1000)
                    // this.fileList = [];
                    this.dialogImageUrl = '';
                },
                handlePictureCardPreview(file, fromData, prop) {
                    console.log(file, fromData, prop)
                    this.dialogImageUrl = file.url;
                    this.dialogVisible = true;
                },
                beforeMasterPictureUpload(params, fromData, prop) {
                    console.log('----aaaa', this.fileList);
                    console.log('file', params, fromData, prop);
                    
                },

这是需求案列

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Vue2+Echarts封装组件之专注逻辑,图表生成自动化方式

    Vue2+Echarts封装组件之专注逻辑,图表生成自动化方式

    文章介绍了使用Vue2封装的Echarts图表组件,简化了图表的生成和渲染过程,提供了多种图表类型和交互功能,提高了开发效率,帮助开发者专注于业务逻辑的开发
    2025-02-02
  • Vue pinia模块化全局注册详解

    Vue pinia模块化全局注册详解

    这篇文章主要介绍了Vue pinia模块化全局注册,Pinia是Vue.js团队成员专门为Vue开发的一个全新的状态管理库,并且已经被纳入官方github
    2023-02-02
  • elementui的el-popover修改样式不生效的解决

    elementui的el-popover修改样式不生效的解决

    在使用element-ui的时候,有一个常用的组件,那就是el-popover,本文就介绍一下elementui的el-popover修改样式不生效的解决方法,感兴趣的可以了解一下
    2021-06-06
  • vue实现购物车列表

    vue实现购物车列表

    这篇文章主要为大家详细介绍了vue实现购物车列表,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-06-06
  • iview同时验证多个表单问题总结

    iview同时验证多个表单问题总结

    这篇文章主要介绍了iview同时验证多个表单问题总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-09-09
  • 解决vuex改变了state的值,但是页面没有更新的问题

    解决vuex改变了state的值,但是页面没有更新的问题

    这篇文章主要介绍了解决vuex改变了state的值,但是页面没有更新的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-11-11
  • 在Vue中使用CSS3实现内容无缝滚动的示例代码

    在Vue中使用CSS3实现内容无缝滚动的示例代码

    这篇文章主要介绍了在Vue中使用CSS3实现内容无缝滚动的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • 一起来学习Vue的组件化

    一起来学习Vue的组件化

    这篇文章主要为大家详细介绍了Vue的组件化,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-03-03
  • vue引用json文件的方法小结

    vue引用json文件的方法小结

    这篇文章主要介绍了vue引用json文件,解决怎么从控制台把数据移到json文件中,只需要直接右击复制值即可,需要的朋友可以参考下
    2022-09-09
  • vue-cli+iview项目打包上线之后图标不显示问题及解决方法

    vue-cli+iview项目打包上线之后图标不显示问题及解决方法

    这篇文章主要介绍了解决vue-cli+iview项目打包上线之后图标不显示问题,本文通过两种方法给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-10-10

最新评论