GithubHelp home page GithubHelp logo

file-download's Introduction

文件续传

和断点下载思路相同。

优势:网络中断情况下,下次网络恢复时,可从中断处继续上传/下载。也可手动暂停上传/下载。

断点下载

  • Range请求头:指定请求实体范围,取值范围0-Content-Length之间

  • 206状态码:表示范围请求成功

主要使用http的范围请求,首先获取目标文件的大小,然后将其拆分,设置Range请求头相应的取值。分段请求目标文件,然后将这些目标文件合并

文件续传

和断点下载反过来即可,客户端负责拆分文件,然后依次向服务端请求,服务端将传来的文件拼接成一个大文件

  • 首先会判断本地localStorage是否有该文件的数据,如果有,则直接修改DOM,从中断处继续上传

  • 然后会判断文件修改时间,如果文件有改动,会重新上传

  • 根据localStorage中的分段信息,确定当前段号,以及使用slice分割文件

  • 中间使用async/await控制每一段按顺序传(并不是好的策略),后面会将其直接异步将所有片段直接发给服务端,通过标识组合文件

  • 服务端将客户端传来的文件存入tmp文件下,当文件传完将其删除

  • 服务端根据客户端传来的信息,确定文件传输完毕,使用appendFileSync组合文件

// localStorage存储的文件信息
logContent = {
  lastModified: file.lastModified,
  fileName: file.name,
  contentLength: file.size,
  blocks: blist,
  pointer: 0
};

file-download's People

Contributors

qxiaoqi avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.