vue3 el-upload文件上传隐藏文件列表

vue3 el-upload文件上传隐藏文件列表

一般情况根据官方教程直接使用el-upload上传是会显示一个列表在下面如图:
在这里插入图片描述
但有时候需求是在导入后不显示这个列表比如:
在这里插入图片描述
这里只有一个导入按钮,点击之后上传文件,不用显示文件列表。
那废话不多说,直接看代码吧:

      // template部分
            <el-upload
              ref="uploadRef"
              accept=".docx"
              :action="xxx'" // 文件上传接口
              :before-upload="handleBeforeUpload"
              class="upload-demo"
              :limit="1"
              :on-error="onErrorFile"
              :on-success="onSuccessFile"
            >
              <el-button type="primary">导入</el-button>
            </el-upload>
     // js部分
     setup() {
     	const state = reactive({
			uploadRef: null
		})
		 // 文件上传前限制只能上传 docx格式的文件
      const handleBeforeUpload = (file) => {
        const fileType = file.name.substring(file.name.lastIndexOf('.') + 1)
        const isDocx = fileType === 'docx'
        if (!isDocx ) {
            ElMessage('只能上传docx格式的文件哦!')
            return false
        }
        return isDocx 
      }
		
		// 文件上传失败钩子
      const onErrorFile = () => {
        ElMessage.error('文件上传失败')
        state.uploadRef.clearFiles(); //去掉文件列表
      }

		// 文件上传成功钩子
      const onSuccessFile = () => {
        ElMessage.success('文件上传成功')
        state.uploadRef.clearFiles(); //去掉文件列表
      }
		return {
			...toRefs(state),
			handleBeforeUpload ,
			onSuccessFile ,
			onErrorFile ,
		}
     }
		

其实就是拿到el-upload的ref,调用一下clearFiles()就可以了

### Vue3 中使用 Element Plus 的 `el-upload` 组件显示文件上传进度 为了在 Vue3 项目中利用 Element Plus 库中的 `el-upload` 组件展示文件上传过程中的进度条,可以遵循如下方法构建相应的逻辑[^1]。 #### 安装依赖库 首先确认已经安装了必要的包: ```bash npm install element-plus axios ``` #### 创建上传组件模板结构 定义 HTML 结构部分,在页面上放置用于触发文件选择对话框以及呈现上传状态的 UI 控件。 ```html <template> <div class="upload-demo"> <!-- 使用 :on-progress 属性绑定处理函数 --> <el-upload ref="upload" action="/api/upload" multiple :limit="3" :auto-upload="false" :on-change="handleChange" :before-upload="handleBeforeUpload" :http-request="customUpload" :on-error="handleError" :on-success="handleSuccess" :on-exceed="handleExceed" :file-list="fileList" :disabled="isUploading || fileList.length >= limitCount" > <button type="button">点击上传</button> <span slot="tip" v-if="!isUploading && fileList.length < limitCount">最多可选 {{limitCount}} 个文件</span> <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload" :disabled="isUploading || fileList.length === 0">开始上传</el-button> </el-upload> <el-progress v-show="showProgress" :percentage="progressPercentage"></el-progress> </div> </template> ``` #### 编写 JavaScript 部分代码 接下来编写脚本逻辑来控制整个流程并更新视图上的进度指示器。 ```javascript <script setup lang="ts"> import { ref, computed } from &#39;vue&#39;; import axios from &#39;axios&#39;; const upload = ref(null); let isUploading = ref(false); // 正在上传标记 let progressPercentage = ref(0); // 当前进度百分比 let showProgress = ref(false); // 是否显示进度条 let fileList = ref([] as any[]); const limitCount = 3; // 文件数量上限 function handleChange(file: any, fileList: any[]) { console.log(&#39;change&#39;, file, fileList); } async function customUpload(option: any) { const formData = new FormData(); formData.append(&#39;file&#39;, option.file); try { isUploading.value = true; showProgress.value = true; await axios({ url: &#39;/your-server-endpoint&#39;, method: &#39;POST&#39;, data: formData, onUploadProgress: (progressEvent) => { let complete = ((progressEvent.loaded / progressEvent.total) * 100) | 0; progressPercentage.value = Math.min(complete, 100); }, }); handleSuccess(&#39;&#39;, option.file); } catch (error) { handleError(error, option.file); } finally { setTimeout(() => { isUploading.value = false; showProgress.value = false; }, 1500); } } ... </script> ``` 上述示例展示了如何通过监听 `onUploadProgress` 回调获取到实际传输的数据量变化情况,并据此调整前端界面上所见的进度比例[^2]。此外还包含了对于并发限制、错误提示等功能的支持。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jieyucx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值