el-upload 进度条
时间: 2023-08-25 13:09:03 浏览: 198
【JavaScript源代码】vue el-upload上传文件的示例代码.docx
要在el-upload组件中添加进度条,你可以使用`show-upload-list`属性来显示上传列表,并利用插槽来自定义列表的内容。以下是一个示例代码:
```html
<template>
<el-upload
ref="upload"
class="upload-demo"
action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
:limit="1"
:on-exceed="handleExceed"
:auto-upload="false"
:show-upload-list="true"
:on-progress="handleProgress"
:on-preview="handlePreview"
>
<template #trigger>
<el-button type="primary">select file</el-button>
</template>
<el-button class="ml-3" type="success" @click="submitUpload">
upload to server
</el-button>
<template #tip>
<div class="el-upload__tip text-red">
limit 1 file, new file will cover the old file
</div>
</template>
<template #default="{ file }">
<div class="el-upload-list__item is-uploading">
<div class="el-upload-list__item-thumbnail">
<img :src="file.url" alt="">
</div>
<div class="el-upload-list__item-content">
<span class="el-upload-list__item-name">{{ file.name }}</span>
<el-progress class="el-upload-list__item-progress" :percentage="file.percentage" />
</div>
</div>
</template>
</el-upload>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { genFileId } from 'element-plus';
import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus';
const upload = ref<UploadInstance>();
const handleExceed: UploadProps['onExceed'] = (files) => {
upload.value!.clearFiles();
const file = files[0] as UploadRawFile;
file.uid = genFileId();
upload.value!.handleStart(file);
};
const handleProgress: UploadProps['onProgress'] = (event, file, fileList) => {
file.percentage = (event.loaded / event.total) * 100;
};
const submitUpload = () => {
upload.value!.submit();
};
export default {
setup() {
return { upload, handleExceed, handleProgress, submitUpload };
},
};
</script>
```
在上述代码中,我们使用了`show-upload-list`属性来显示上传列表,并使用默认插槽来定义列表的内容。在列表项中,我们添加了一个进度条组件`<el-progress>`来展示文件上传的进度。`on-progress`事件监听文件的上传进度,将进度值赋给文件对象的`percentage`属性,在模板中使用该属性来显示进度条。
请注意,这里使用的是Vue 3的`<script setup>`语法,你可以根据自己的项目配置进行调整。
阅读全文