大文件切片上传-vue3.0

大文件切片上传(也称为分片上传)是一种处理大文件上传的有效方法,它通过将大文件分割成多个较小的部分(即切片或分片),然后分别上传这些切片到服务器,最后在服务器上将这些切片合并成原始文件。以下是关于大文件切片上传的详细步骤和注意事项

切片上传核心流程

🌟 确定切片大小

🌟 文件切片(File.slice())

🌟 并发控制 分片上传

🌟 验证文件完整性

基于vue3.0代码实现

<template>
    <el-upload
        v-model:file-list="fileList"
        v-bind="getAttrs($attrs)"
        :on-success="onSuccess"
        :on-error="onError"
        :on-progress="onProgress"
    >
        <template #trigger>
            <el-button type="primary">选择文件</el-button>
        </template>
    </el-upload>
    <el-progress :percentage="percentage ?? 0" v-if="percentage > 0"></el-progress>
</template>

<script setup>
import {computed, reactive} from 'vue';
import SparkMD5 from 'spark-md5';
// import {ApiInitUpload,  ApiUploadChunk, ApiUploadFinish} from './api';
import {CONST} from './config';
defineOptions({
    name: 'PlUpload'
});
const emit = defineEmits(['upldate:modelValue']);

const props = defineProps({
    uploadMode: {
        type: String,
        default: 'default',
    },
    chunkSize: {
        type: Number,
        default: () => 5 * 1024 * 1024,
    },
    data: {
        type: Object,
        default: () => {
            return {};
        },
    },
    limit: {
        type: Number,
    },
    modelValue: {
        type: Array,
    },
    onSuccess: {
        type: Function,
    },
    onError: {
        type: Function,
    },
    onProgress: {
        type: Function,
    },
    onUploadInit: {
        type: Function,
    },
    onUploadFinish: {
        type: Function,
    },
    onUploadChunk: {
        type: Function,
    },
});

const uploadProgress = reactive({
    progress: [],
    total: 0,

});

const fileList = computed({
    get: () =>{
        return props.modelValue;
    },
    set: (val) => {
        console.log('set', val)
        return emit('update:modelValue', val);
    },
});

async function onUpload(response) {
    const chunkList = getChunkList(response.file, props.chunkSize);
    const hash = await getChunkHash(chunkList);
    uploadProgress.total = chunkList.length;

    init(hash);
    uploadChunks(chunkList, hash, response.file.name);
};
function getChunkHash(chunkList) {
    const spark = new SparkMD5();
    const fileReader = new FileReader();
    let currentChunk = 0;

    return new Promise((resolve, reject) => {

        fileReader.readAsArrayBuffer(chunkList[currentChunk]);

        fileReader.onload = (e) => {
            currentChunk++;
            if (currentChunk < chunkList.length) {
                spark.append(e.target.result);
                fileReader.readAsArrayBuffer(chunkList[currentChunk]);
            }
            else {
                resolve(spark.end());
            }
        };
    });

}
function getChunkList(file, chunkSize) {

    const blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice;

    const chunks = Math.ceil(file.size / chunkSize);

    const chunkList = [];

    for(let i = 0; i < chunks; i++) {
        chunkList.push(blobSlice.call(file, i * chunkSize, (i + 1) * chunkSize));
    }

    return chunkList;

}
// 通知开始上传
function init() {
    props?.onUploadInit();
}
// 上传接口
function uploadChunks(chunkList, hash, name) {
    const requestList = chunkList.map((item, index) => {
        const formData = new FormData(); 
        formData.append('file', item);
        formData.append('hash', hash);
        formData.append('file_name', `${index}_${name}`);
        // 添加上传的额外参数
        for (const key in props.data ?? {}) {
            formData.append(key, props.data[key]);
        };
        // return ApiUploadChunk(formData, arg => onUploadProgress(arg, index));
        return props?.onUploadChunk(formData, arg => onUploadProgress(arg, index));
    });
    Promise.all(requestList).then(res => {
        uploadFinish(hash);
    }).catch(err => {
        hubEvent(CONST.ON_ERROR, err);
        throw err;
    });
}

function onUploadProgress(payload, index) {
    uploadProgress.progress[index] = payload.progress;
    hubEvent(CONST.ON_PROGRESS);
};

// 上传完成
async function uploadFinish(hash) {
    try {
        const data = await props?.onUploadFinish({hash});
        hubEvent(CONST.ON_SUCCESS, data);
    }
    catch (err){
        hubEvent(CONST.ON_ERROR, err);
        throw err;
    } 
}

const percentage = computed(() => {
    const {progress, total} = uploadProgress;
    const count = progress?.reduce((total, current) => {
        total+=current;
        return total;
    }, 0);
    return Number(((count / total) * 100)?.toFixed() ?? 0);
});

// utils
function hubEvent(name, data) {
    const event = props[name];
    if (event) {
        event(data);
    }
}

function getAttrs(attrs) {
    const newAttrs = {
        ...attrs,
        [CONST.HTTP_REQUEST]: onUpload,
    }
    if (props.mode !== CONST.MODE_CHUNK) {
        Reflect.deleteProperty(newAttrs, CONST.HTTP_REQUEST);
        Reflect.set(newAttrs, CONST.LIMIT, 1);
    }
    return newAttrs;
}
</script>

配置文件

/**
*config
*/
export const CONST = {
    'MODE_CHUNK': 'chunk',
    'LIMIT': 'limit',
    'HTTP_REQUEST': 'http-request',
    'ON_SUCCESS': 'on-success',
    'ON_ERROR': 'on-error',
    'ON_PROGRESS': 'on-progress',
}

名次解释

SparkMD5:生成文件唯一的hash,用来验证文件的完整性

File.prototype.slice:用于文件进行切片

onUploadInit: 文件初始化调用接口,用于后端创建存放切片文件的文件夹

onUploadChunk:分片上传接口

onUploadFinish:文件上传完成验证完整性的接口

webwork:多线程,提升性能

相关推荐
小阮的学习笔记8 分钟前
Vue3中使用LogicFlow实现简单流程图
javascript·vue.js·流程图
YBN娜9 分钟前
Vue实现登录功能
前端·javascript·vue.js
阳光开朗大男孩 = ̄ω ̄=9 分钟前
CSS——选择器、PxCook软件、盒子模型
前端·javascript·css
杨荧11 分钟前
【JAVA毕业设计】基于Vue和SpringBoot的服装商城系统学科竞赛管理系统
java·开发语言·vue.js·spring boot·spring cloud·java-ee·kafka
minDuck13 分钟前
ruoyi-vue集成tianai-captcha验证码
java·前端·vue.js
小政爱学习!34 分钟前
封装axios、环境变量、api解耦、解决跨域、全局组件注入
开发语言·前端·javascript
魏大帅。39 分钟前
Axios 的 responseType 属性详解及 Blob 与 ArrayBuffer 解析
前端·javascript·ajax
花花鱼1 小时前
vue3 基于element-plus进行的一个可拖动改变导航与内容区域大小的简单方法
前端·javascript·elementui
k09331 小时前
sourceTree回滚版本到某次提交
开发语言·前端·javascript
web行路人1 小时前
React中类组件和函数组件的理解和区别
前端·javascript·react.js·前端框架