【Element UI】el-upload 踩的坑

76 篇文章 0 订阅
27 篇文章 2 订阅

1、action

上传的地址,接口地址
直接在 action 中写后端地址会出现跨域问题,而且传参数不方便
就用 http-request 指定具体上传方法

2、auto-upload

是否在选取文件后立即进行上传,默认 true
在 action 赋空值,使用 http-request 指定方法上传时,auto-upload 为 false

3、http-request

覆盖默认的上传行为1,可以自定义上传的实现
默认的上传方法均失效,获取不到 file 值
需要使用 on-change2 做上传文件前的处理

4、上传文件显示进度条

el-progress3

5、上传 .xls , .xlsx 文件并显示进度条的实现代码

<el-dialog
  ref=""
  append-to-body
  :title="excel.title"
  v-if="excel.visible"
  :close-on-click-modal="false"
  :visible.sync="excel.visible"
>
  <el-upload
    class="upload-demo"
    ref="upload"
    action=""
    :limit="1"
    :auto-upload="false"
    :file-list="excel.upload.fileList"
    :on-change="excelChange"
    :http-request="excelRequest"
    :on-remove="excelRemove"
  >
    <el-button icon="el-icon-upload2" plain @click="excelReset">{{ st('frame.choiceFile') }}</el-button>
    <div slot="tip" class="el-upload__tip">只能上传 .xlsx 和 .xls 文件,且不超过1个文件</div>
  </el-upload>
  <el-progress v-show="excel.progressFlag" :percentage="excel.loadProgress"></el-progress>
  <div ref="uploadFile"></div>
  <div slot="footer" class="dialog-footer">
    <el-button type="primary" @click="cancel()">{{st('frame.cancel')}}</el-button>
    <el-button type="success" @click="submitFile()">{{st('publicCustom.ok')}}</el-button>
  </div>
</el-dialog>
data() {
	return {
      excel: {
        title: this.st('frame.import'),
        visible: false,
        progressFlag: false,
        loadProgress: 0,
        upload: {
          fileList: [],
          action: '',
          headers: {},
          data: {
            jsondata: ''
          }
        }
      }
   }
}
methods: {
	excelReset() {
      this.excel.upload.fileList = []
      this.$refs.uploadFile.innerHTML = null
    },
    excelRemove() {
      this.excel.upload.fileList = []
      this.excel.progressFlag = false
      this.$refs.uploadFile.innerHTML = null
      this.excel.loadProgress = 0
    },
    excelChange(file) {
      if (file.name.indexOf('.xlsx') == -1 && file.name.indexOf('.xls') == -1) {
        this.$message.error(this.st('frame.uploadError'))
        this.excel.upload.fileList = [] 
      } else {
        if(file.status === 'ready'){
          this.excel.progressFlag = true
          this.excel.loadProgress = 0
          const interval = setInterval(() => {
            if(this.excel.loadProgress >=99){
              clearInterval(interval)
              return
            }
            this.excel.loadProgress += 1
          }, 20)
        }
        this.excelRequest(file)
      }
    },
    excelRequest(file) {
      var form = new FormData()
      form.append("file", file.raw)
      InportPbomPartExcel(form).then((res) => {
        if (res.resultData.success) {
          const url = settings.api.url + res.resultData.fileName
          window.open(url)
          let template = res.resultData.MessageString
          this.$refs.uploadFile.innerHTML = template
          this.excel.progressFlag = false
          this.excel.loadProgress = 100
        } else {
          this.$message.error(res.resultData.MessageString)
        }
      })
    }
}

  1. on-preview 点击文件列表中已上传的文件时的钩子
    on-success 文件上传成功时的钩子
    on-progress 文件上传时的钩子
    before-upload 上传文件之前的钩子,参数为上传的文件,若返回 false 或者返回 Promise 且被 reject,则停止上传。 ↩︎

  2. on-change 文件状态改变时的钩子,添加文件、上传成功和上传失败时都会被调用 ↩︎

  3. <el-upload 
     action="Fake Action" :before-upload="uploadSuccess" :on-change="uploadVideoProcess" :show-file-list="false" :file-list="fileList">
     <el-button v-if="typePage !=='view'" size="mini" type="primary">点击上传</el-button>
     <span v-if="typePage !=='view'" slot="tip" class="el-upload__tip">支持pdf,jpg,png格式文件</span>
    </el-upload>
    <el-progress v-show="progressFlag" :percentage="loadProgress"></el-progress>
    
    uploadVideoProcess(file, fileList) { 
       if(file.status === 'ready'){
          this.progressFlag = true; // 显示进度条
          this.loadProgress = 0; 
          const interval = setInterval(() => {
            if(this.loadProgress >=99){
              clearInterval(interval)
              return
            }
            this.loadProgress += 1
          }, 20);
        }
      if (file.status === 'success') {
        this.progressFlag = false; // 不显示进度条
        this.loadProgress = 100;
      }
    }
    
    ↩︎
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Element UI中的el-upload是一个文件上传组件,用于在网页上实现文件上传功能。可以通过配置不同的属性和事件来自定义上传的行为。 在引用中的示例中,el-upload组件的配置包括: - accept属性指定只能上传.png和.jpg格式的文件。 - :limit属性指定最多可以选择5个文件。 - :on-exceed属性指定当选择的文件数量超过限制时的回调函数。 - :file-list属性指定显示已选择文件的列表。 - :on-change属性指定选择文件后的回调函数。 - :auto-upload属性设置为false,表示不自动上传文件。 - :http-request属性指定上传文件的请求方法。 在引用中的示例中,与引用相比,去除了:uploadFile属性,即上传文件的请求方法。 在引用中的示例中,el-upload组件用于上传头像。其中的配置包括: - action属性指定上传文件的接口。 - :show-file-list属性设置为false,表示不显示已上传文件的列表。 - :on-success属性指定上传成功后的回调函数。 - :before-upload属性指定在上传文件之前的操作,如验证文件类型和大小等。 总而言之,el-uploadElement UI中提供的一个文件上传组件,可以根据需求配置属性和事件来实现不同的上传功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [elementUIel-upload的使用以及遇到的(手动上传案例)](https://blog.csdn.net/fangqi20170515/article/details/131393214)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Element-UIel-upload上传组件(demo详解)](https://blog.csdn.net/weixin_45393094/article/details/111407514)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值