文件分片上传demo

知识点

  • File
    File 接口也继承了 Blob 接口的属性
    File 接口没有定义任何方法,但是它从 Blob 接口继承了以下方法:
    Blob.slice([start[, end[, contentType]]])
    new File([“字符串数组”,fileName)
    .repeat(n) 将原字符串重复n次,返回一个新的字符串

  • Formdata 为序列化的表单
    var formData = new FormData();
    formData.append(“username”, “ccy”);
    formData 作为传给服务器的数据

参考链接: link

测试接口:http://httpbin.org/

代码

在这里插入图片描述

<script setup>
import { ref } from 'vue';



const file = new File(['test'.repeat(3)], 'test.txt')

// 分片大小
const size = 3
let url = 'http://httpbin.org/post'

// 实际工作中,需要给每片加id进行判断

function chunkFn() {
  
  for(let i = 0; i< file.size; i+=size){
    const chunks = file.slice(i, i+size)
    console.log(chunks)

   
    let upData = new FormData()
    upData.append('chunkData', chunks)

    fetch(url, {method:'post', body:upData})
      .then(response => 
        response.json()
      ).then(data => {
        console.log(data.files)
      })
      
  }

}

</script>

<template>
<div>File ,分片文件</div>
<button @click="chunkFn">测试分片按钮</button>
</template>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Vue 文件分片的代码示例: ``` <template> <div> <el-upload class="upload-demo" action="/upload" :before-upload="beforeUpload" :on-progress="onProgress" :on-success="onSuccess" :on-error="onError" :headers="headers" :data="{chunkIndex: chunkIndex, chunkTotal: chunkTotal, fileName: fileName}" > <el-button size="small" type="primary">选择文件</el-button> </el-upload> </div> </template> <script> import axios from 'axios' import { Message } from 'element-ui' export default { data() { return { file: null, fileName: '', chunkSize: 2 * 1024 * 1024, // 每个分片的大小 chunkIndex: 0, // 当前上分片索引 chunkTotal: 0, // 分片总数 progress: 0, // 上进度 headers: { 'Content-Type': 'application/octet-stream' // 指定文件类型为二进制流 } } }, methods: { // 选择文件 beforeUpload(file) { this.file = file this.fileName = file.name this.chunkTotal = Math.ceil(file.size / this.chunkSize) }, // 上进度 onProgress(event) { this.progress = Math.round((100 * event.loaded) / event.total) }, // 上成功 onSuccess(response) { if (this.chunkIndex === this.chunkTotal - 1) { // 如果上的是最后一个分片,表示文件成功 Message.success('文件成功') } else { // 否则继续上下一个分片 this.chunkIndex++ this.uploadChunk() } }, // 上失败 onError(error) { Message.error('文件失败') }, // 上分片 uploadChunk() { const start = this.chunkIndex * this.chunkSize const end = Math.min((this.chunkIndex + 1) * this.chunkSize, this.file.size) const chunk = this.file.slice(start, end) const formData = new FormData() formData.append('file', chunk) axios.post('/upload', formData, { headers: { 'Content-Type': 'multipart/form-data' }, params: { chunkIndex: this.chunkIndex, chunkTotal: this.chunkTotal, fileName: this.fileName } }) .catch(() => { Message.error('文件失败') }) } } } </script> ``` 需要注意的是,上述代码仅为示例,具体实现需根据实际需求进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值