Vue文件上传前端代码

本文详细介绍了如何在Vue项目中使用el-upload组件进行文件上传,包括模板结构、事件处理函数和父子组件通信,以及如何在form表单中引用和控制上传行为。
摘要由CSDN通过智能技术生成

一、编码上传组件

<template>
    <div>
      <div v-for="item in list" style="width:100%;display: flex;">
        <label style=" font-weight: 550;width: 185px; font-size: 12px;text-indent: 2em;color: #333;line-height: 32px;">{{item.label}}</label>
        <span style="margin-left: 20px;width: 72%;display: inline-block;">
             <el-upload
               :action="'/fosung-village-shandong/api/common/upload_files?fk=2qu2r9hppsittpkpsgnrtf40ka'"
               :on-preview="handlePreview"
               :on-remove="handleRemove"
               :limit="10"
               :on-success="success"
               :on-error="error"
             accept=".png,.pdf,.jpg,.jpeg,.doc,.xls"
               :before-upload="beforeUpload"
               :file-list="fileList"
               class="upload-demo"
            style="width: 100%;"
               multiple>
            <button-base text="上传文件" type="primary" :disabled="disabled"/>
          </el-upload>
          </span>
      </div>
    </div>
</template>

<script>
  import buttonBase from '../../components/Button/support/button-base'
  export default {
    props:['list','fileList',"index"],
    components:{
      buttonBase
    },
    data(){
      return{
        // fileList:[],
        uploadList:[],
        disabled:true,
      }
    },
    methods:{
      success(res){
        if(res.code == 200){
          res.data.files.forEach(file =>{
            this.fileList.push({
              name: file.attaName,
              url: file.fullPath,
              attaPath: file.attaPath,
              id: '',
              operFlag: '0'
            })
          })
          this.$emit('sendFile',this.fileList)
          this.$emit('controll',false,this.index)
        }else{
          this.fileList.splice(Number(this.fileList.length-1),1)
          this.$notify({
            title: '失败',
            message: res.msg,
            type: 'error',
            duration: 2000
          })
        }
      },
      beforeUpload(res){
        this.$emit('controll',true)
      },
      error(err){
        console.log(err,'1111')
      },
      handleRemove(file, fileList) {
        for (let i = 0; i < this.fileList.length; ++i) {
          if (file.id == this.fileList[i].id) {
            this.fileList[i].operFlag = 1
          }
        }
        this.$emit('sendFile',this.fileList)
      },
      handlePreview(file){
        var index = file.name.lastIndexOf('\.')
        const data = file.name.substring(index + 1, file.name.length)
        window.open(file.url)
      },
      parentMsg(data){
        this.disabled = data;
      }
    }
  }
</script>

<style scoped>

</style>

二、导入上传组件

import upload from './upload'//上传
在form表单中引用
<div v-if="table.type == 'upload'" style="display: flex;">
  <upload :list="uploadList" :fileList="fileList" ref="upload"         @controll="controll" :index="index"  
     @sendFile="acceptFile"></upload>
</div>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值