GithubHelp home page GithubHelp logo

itsharex / uploader Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tiyee/uploader

0.0 0.0 0.0 42 KB

大文件上传前后端方案,支持秒传,分片传输,断点续传。支持h5和微信小程序

License: MIT License

Shell 0.21% JavaScript 26.75% Go 42.82% TypeScript 22.94% Makefile 0.54% HTML 6.74%

uploader's Introduction

Uploader

大文件上传插件,支持秒传,分片上传,断点续传。

Install

  1. html页面直接引用dist文件,用法参考demo.html
  2. es环境可以直接引用dist文件夹的index.esm.js文件,也可以直接引用src/uploader.tsx

使用方法(html)

<body >
<div id="root">
    <input type="file" id="upload" onchange="upload(this.files)" />
</div>
<script src="/dist/index.js"></script>
<script>
    document.getElementById( "upload" ).onchange  =  function(e){
        console.log(Uploader);
        const file = e.target.files[0]
            const ctx = {
                maxConcurrency: 5,
                totalSize: file.size,
                chunkSize: 1024 * 1024,
                uploadUrl: '/2/upload',
                mergeUrl: '/2/merge',
                touchUrl: '/2/init',
                testChunks: false,
                withCredentials: 'include',
            }
            const up = new Uploader(ctx, file)
            up.on('progress', (e) => {
                console.log('progess',e)
            })
            up.on('complete', (e) => {
                console.log('complete',e)
                alert(e.url)
            })
            up.run()
    }
        
</script>
</body>

监听事件

  • success,上传成功时触发,e = {"upload_id":"xxxxx","status":1,"url":"","chunks":[]},其中status定义0:待上传,1:上传中,2:已完成,3:已失败

  • fail,上传失败时触发,e :Error

  • complete,上传成功或失败时触发,返回值同 success 或 fail

  • progess,上传进度变化时触发,返回内容如下e={ uploadedSize: 1286679, totalSize: 1286679 }

服务端接口

touchUrl (post)

初始化上传动作,返回upload_id,会携带文件md5和mime信息,

如果文件已经存在,则直接返回status=2,url不为空。

如果之前上传过,status=1,chunks会返回已经上传的chunk的index和hash

ur参数

属性 类型 说明
size String 整个文件的 md5 值
chunk_size String 整个文件的大小
digest String 整个文件的 md5 值

body

文件的前200个字节(为了获取文件的mime)

返回参数

属性 类型 说明
url String 已上传时返回线上文件路径
upload_id String 上传凭证,如果已完成,该字段为空字符串
chunks Array<{index:number,etag:string}> 未完全上传时,返回已上传的分块序号
status int 0:待上传,1:上传中,2:已完成,3:已失败

uploadUrl (post)

分片传输

ur参数

属性 类型 说明
index String 分片序号,从0开始
upload_id String 传输凭证
digest String 分片内容的 md5 值

body

传输的分片内容

mergeUrl

合并传输分片

ur参数

属性 类型 说明
upload_id String 传输凭证
digest String 整个文件的 md5 值

body

json格式,所有分片的编号和摘要,Array<{index:number,etag:string}>

微信小程序

用法与h5一致,参考wxm文件夹下的代码

uploader's People

Contributors

tiyee 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.