【vue2+elementui】记录el-upload导入文件:只上传一个文件,且再次上传会覆盖上一个文件

使用on-change实现,因为:文件状态改变时的钩子,添加文件、上传成功和上传失败时都会被调用

javascript 复制代码
<div class="batch-import-contain">
  <el-button type="primary" size="mini">下载模板</el-button>
  <el-upload
    class="upload-demo"
    drag
    action="https://jsonplaceholder.typicode.com/posts/"
    accept=".xls,.xlsx,.csv,.et,.ett,.xlt"
    :multiple="false"
    :file-list="fileList"
    :on-change="onchange"
  >
    <i class="el-icon-upload"></i>
    <div class="el-upload__text">
      <p>点击或将文件拖拽到这里上传</p>
      <p>支持扩展名:.xls .xlsx .csv .et .ett .xlt...</p>
    </div>
  </el-upload>
</div>
javascript 复制代码
export default {
  name: 'batchImport',
  data() {
    return {
      fileList: [],
    }
  },
  methods: {
    onchange(file, fileList) {
      console.log(file, fileList, 'file, fileList)')
      if(fileList.length > 1) {
		fileList.splice(0, 1)	// 将第一个删掉
	  }
	  // 写在if外面,因为:第一个只有一个文件时也许将fileList保存在this.fileList里面
	  this.fileList = fileList
    },
  },
}
相关推荐
passerby60612 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了2 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅2 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅2 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅3 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment3 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅3 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊3 小时前
jwt介绍
前端
爱敲代码的小鱼3 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax
Cobyte4 小时前
AI全栈实战:使用 Python+LangChain+Vue3 构建一个 LLM 聊天应用
前端·后端·aigc