element-ui大文件上传 单个文件

<template>
  <el-upload ref="uploadRef" class="upload-demo" action="" :auto-upload="false" :show-file-list="false"
    :http-request="handleUploadRequest" :before-upload="handleBeforeUpload">
    <template #trigger>
      <el-button type="primary">选择文件</el-button>
    </template>
    <el-button class="ml-3" type="success" @click="submitUpload">
      上传文件{{ progressNum }}
    </el-button>
    <template #tip>
      <div class="el-upload__tip">
        文件大小限制 500kb
      </div>
    </template>
  </el-upload>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import type { UploadInstance } from 'element-plus';

const uploadRef = ref<UploadInstance>();
// 文件对象
const fileValue = ref<File | null>(null);
// 分片大小
const chunkSize = ref<number>(1024 * 1024);
// 总分片数量
const totalChunks = ref<number>(0);
// 当前分片索引
const currentChunk = ref<number>(0);
// 进度条值
const progressNum = ref<number>(0);

// 设置beforeUpload钩子函数以获取选中的文件
const handleBeforeUpload = (file: File) => {
  console.log(file, '-00-0-0-11111');
  fileValue.value = file;
  handleFileChange(file);
  return false; // 阻止默认上传行为,因为我们使用自定义的http-request
};

// 在选择文件后计算总分片数
const handleFileChange = (file: File) => {
  console.log(file, '-0-0-0-0');
  if (!file) return;
  totalChunks.value = Math.ceil(file.size / chunkSize.value);
};

// 提交上传
const submitUpload = () => {
  uploadRef.value!.submit()
  if (fileValue.value) {
    currentChunk.value = 0;
    handleUploadRequest();
  }
};

// 自定义上传请求方法
const handleUploadRequest = async () => {
  if (!fileValue.value || currentChunk.value >= totalChunks.value) return;

  const start = currentChunk.value * chunkSize.value;
  const end = Math.min(fileValue.value.size, start + chunkSize.value);
  const chunk = fileValue.value.slice(start, end);

  const formData = new FormData();
  formData.append('file', chunk as Blob);
  formData.append('chunk', currentChunk.value.toString());
  formData.append('totalChunks', totalChunks.value.toString());
  formData.append('fileName', fileValue.value.name);

  if (true) {
    // 更新当前分片索引和进度
    currentChunk.value++;
    progressNum.value = (currentChunk.value / totalChunks.value) * 100;
    // 如果所有分片都已上传,则触发最终的完成逻辑(此处仅作示例,实际应用中根据服务器返回进行处理)
    if (currentChunk.value === totalChunks.value) {
      console.log('全部分片已上传完成');
    }
  } else {
    console.error('Error uploading chunk');
  }
};
</script>

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Element-UI 是一套基于 Vue.js 的桌面端组件库,它提供了丰富的 UI 组件和交互效果,可以帮助开发者快速构建美观、易用的前端界面。使用 Element-UI 需要先安装 Vue.js,并在项目中引入 Element-UI 的相关组件。 以下是使用 Element-UI 的一般步骤: 1. 安装 Vue.js:在项目中使用 Element-UI 之前,需要先安装 Vue.js。可以通过 npm 或者 yarn 进行安装,具体命令如下: ``` npm install vue ``` 或者 ``` yarn add vue ``` 2. 安装 Element-UI:安装完 Vue.js 后,可以通过 npm 或者 yarn 安装 Element-UI。具体命令如下: ``` npm install element-ui ``` 或者 ``` yarn add element-ui ``` 3. 引入 Element-UI 组件:在项目的入口文件中,引入需要使用的 Element-UI 组件。可以按需引入单个组件,也可以引入全部组件。例如,引入 Button 组件的代码如下: ```javascript import { Button } from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; Vue.use(Button); ``` 4. 使用 Element-UI 组件:在项目的页面中,可以直接使用已经引入的 Element-UI 组件。例如,在模板中使用 Button 组件的代码如下: ```html <template> <div> <el-button type="primary">按钮</el-button> </div> </template> ``` 以上是使用 Element-UI 的一般步骤,你可以根据具体需求引入和使用其他的 Element-UI 组件。如果需要更详细的使用说明和示例代码,可以参考 Element-UI 的官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

猛男敲代码

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

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

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

打赏作者

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

抵扣说明:

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

余额充值