前端实现边下载文件边上传

问题记录原因:

因为需要实现网络文件的上传,结果是由前端实现,方式是一边下载,一遍上传文件,小文件直接上传,大文件进行切片,切片大小和下载大小有关,特此记录。

1.实现方案

fetch进行网络连接的下载,在请求的返回对象中调用response.body.getReader(),这里是读取文件流,便于监听,读取的方式为reader.read(),它的返回对象中包含,文件对象和完成状态,这样就能实现,循环执行下载的动作知道,状态为完成停止。

2.步骤代码

1)下载

复制代码
const response: any = await fetch(downloadUrl);
const reader = response.body.getReader();
// 读取响应的文件字节数
const contentLength = +response.headers.get('Content-Length');

2)上传

使用MD5计算文件的hash进行分片上传

复制代码
let receivedLength = 0;
// 分片上传的话需要计算每一片文件的hash,我们使用MD5
const spark = new SparkMD5.ArrayBuffer();
while(true) {
     const {done, value} = await reader.read();
     if (done) {
         // 计算整个文件的hash
         const finalHash = spark.end();
         console.log('下载&上传完成:', finalHash);
         break;
     }
     // 更新整个文件的hash
     spark.append(value);
     receivedLength += value.length;
     console.log(`下载进度: ${receivedLength} / ${contentLength}`);
      
     // 创建一个新的SparkMD5实例来处理当前分片
     const chunkHash = SparkMD5.ArrayBuffer.hash(value); // 计算当前分片的hash
     const offset = receivedLength - value.length;
     // console.log(contentLength, offset, chunkHash, hash, value.length);
     // 上传这个分片,这里执行接口
     ...
}

contentLength, offset, chunkHash, hash, value.length,这些参数是分片上传所需要的,依次是:

文件的大小,分片的偏移量,每一片的hash(这个参数可选),整个文件的hash(这个需要提前计算出来),每一个分片的大小;

3.拓展

1)计算大文件的hash

可以参考:【文件比较】前端上传比较内容及名字-CSDN博客

2)content-type类型与文件类型的对应关系

不全面,只是涵盖大多文件类型:

复制代码
const getFileTypeByContentType = (contentType: string) => {
    let type = ['image/jpeg', 'image/pjpeg'].includes(contentType) ? 'jpg':
        ['image/png'].includes(contentType) ? 'png':
        ['image/gif'].includes(contentType) ? 'gif':
        ['image/svg+xml'].includes(contentType) ? 'svg':
        ['video/mp4'].includes(contentType) ? 'mp4':
        ['video/quicktime'].includes(contentType) ? 'mov':
        ['text/html'].includes(contentType) ? 'html':
        ['text/markdown'].includes(contentType) ? 'md':
        ['text/plain'].includes(contentType) ? 'txt':
        ['text/csv'].includes(contentType) ? 'csv':
        ['application/json'].includes(contentType) ? 'json':
        ['application/x-yaml', 'text/yaml'].includes(contentType) ? 'yaml':
        ['application/pdf'].includes(contentType) ? 'pdf':
        ['application/msword'].includes(contentType) ? 'doc':
        ['application/vnd.openxmlformats-officedocument.wordprocessingml.document'].includes(contentType) ? 'docx':
        ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'].includes(contentType) ? 'xlsx':
        ['application/vnd.ms-excel'].includes(contentType) ? 'xls':
        ['application/vnd.openxmlformats-officedocument.presentationml.presentation'].includes(contentType) ? 'pptx':
        ['application/zip'].includes(contentType) ? 'zip':
        '';
    return type
}
相关推荐
mldong2 小时前
你的 Vue3 项目也能有钉钉同款审批流设计器:npm 装包,10 分钟画出第一条审批流
前端·vue.js
2分钟速写快排3 小时前
什么是 RAG?如何用 RAG 实现一个用户记忆?
前端·后端·ai编程
passerby60614 小时前
如何自己造一个时间处理库
前端·javascript·github
走到天涯海角5 小时前
react里面的长列表渲染优化
前端·react.js·前端框架
小羊没烦恼!5 小时前
Hello Web API系列教程——Web API与国际化
java·服务器·前端·javascript·php
北岛贰5 小时前
迷茫焦虑期,我做了一个带支付带官网的 AI 聊天虚拟恋人 App
前端·人工智能·后端
mayaairi6 小时前
Vue2 组件通讯(三):全局事件总线、PubSub、插槽与组件实例属性
前端·javascript·vue.js
kyriewen7 小时前
面试官问我:AI 都能写代码了,前端凭什么还值 25K
前端·javascript·人工智能
风骏时光牛马8 小时前
AI源码分析:拆解模型底层实现逻辑
前端
IT_陈寒8 小时前
React子组件莫名其妙重渲染?你可能漏了这个Hook
前端·人工智能·后端