前端js大型文件切片上传

html

<input type="file" id="videoInput" accept="video/*" />
<button id="uploadButton">上传视频</button>
<div id="progress"></div>

 js 

// 切片大小(例如 5MB)
const CHUNK_SIZE = 5 * 1024 * 1024;

document.getElementById('uploadButton').addEventListener('click', async () => {
    const file = document.getElementById('videoInput').files[0];
    if (!file) {
        alert('请选择视频文件');
        return;
    }

    // 文件切片
    const chunks = createFileChunks(file, CHUNK_SIZE);

    // 上传所有切片
    for (let i = 0; i < chunks.length; i++) {
        const chunk = chunks[i];
        const formData = new FormData();
        formData.append('file', chunk);
        formData.append('filename', file.name);
        formData.append('chunkIndex', i);
        formData.append('totalChunks', chunks.length);

        try {
            await uploadChunk(formData, i, chunks.length);
            console.log(`切片 ${i + 1}/${chunks.length} 上传成功`);
        } catch (error) {
            console.error(`切片 ${i + 1}/${chunks.length} 上传失败:`, error);
            break;
        }
    }

    console.log('所有切片上传完成');
});

// 将文件切片
function createFileChunks(file, chunkSize) {
    const chunks = [];
    let start = 0;
    while (start < file.size) {
        const chunk = file.slice(start, start + chunkSize);
        chunks.push(chunk);
        start += chunkSize;
    }
    return chunks;
}

// 上传切片
function uploadChunk(formData, chunkIndex, totalChunks) {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.open('POST', '/upload', true);

        // 监听上传进度
        xhr.upload.onprogress = (event) => {
            if (event.lengthComputable) {
                const percent = ((chunkIndex + event.loaded / event.total) / totalChunks * 100).toFixed(2);
                document.getElementById('progress').innerText = `上传进度: ${percent}%`;
            }
        };

        xhr.onload = () => {
            if (xhr.status === 200) {
                resolve(xhr.responseText);
            } else {
                reject(new Error('上传失败'));
            }
        };

        xhr.onerror = () => {
            reject(new Error('上传失败'));
        };

        xhr.send(formData);
    });
}

 拓展

视频预览

const video = document.createElement('video');
video.src = URL.createObjectURL(file);
video.controls = true;
document.body.appendChild(video);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值