对接grpc之大文件分片传输(2)-文件切片

题引

利用 Blob.prototype.slice方法进行切片

可以安装这些依赖包

"dependencies": {
    "crypto-js": "^4.0.0",
    "goog": "^0.2.1",
    "google-protobuf": "^3.14.0-rc.2",
    "grpc-web": "^1.2.1",
    "lodash": "^4.17.20"
  }

参数设置

类名:BigFileUpload

设置配置项

BigFileUpload.prototype.options = {
    url: "",
    chunkSize: 10 * 1024 * 1024,  //默认单个切片大小:10M
    concurrency: 10, // 一次最多上传10个,防止大量切片同时上传
  }

设置上传事件,便于页面调用过程中监听

BigFileUpload.prototype.events = ["error", "progress", "cancel", "success"]

grpc连接

引入相关数据,HelloWorld_pb中是相关参数的数据结构,DemoClient是grpc服务的包装类

import {
    UploadPartRequest,
    UserAuthorization,
    FileDescriptor
} from "./HelloWorld_pb"
import {
    DemoService
} from "./HelloWorld_grpc_web_pb"

服务连接

client = new DemoService(options.url, null, null)

上传

接收文件时,构造一个文件对象

chunkNum = Math.ceil(file.size / options.chunkSize)
        for (let i = 0; i < chunkNum; i++) {
            notUploadedPart.push(i)
        }
fileOb = {
            path: path,
            fileName: file.name,
            fileSize: file.size,
            chunkNum: chunkNum,
            uploadPercentage: 0,
            uploadStatus: "uploading", //[uploading,error,success,-----]
            file: file,
            uploadedPart: [],
            notUploadedPart: notUploadedPart,
            runningPart: []
        }

上传单个分片

uploadChunk: function (i) {
        let end =
            (i + 1) * options.chunkSize >= fileOb.fileSize ?
            fileOb.fileSize :
            (i + 1) * options.chunkSize,
            request = new UploadPartRequest(),
            reader = new FileReader()
        request.setPartOrder(i + 1)
        reader.onload = function () {
            request.setBody(new Uint8Array(reader.result))
            client.mpuUploadPart(request, null, (err, response) => {
                if (fileOb.uploadStatus === "aborted" || fileOb.uploadStatus === "error") {
                    return
                } 
                
                fileOb.runningPart.splice(fileOb.runningPart.indexOf(i), 1)
                if (err) {             
                    self.postMessage({
                            type: "error",
                            content: “*****”
                        })
                     
                    }
                } else {
                    fileOb.uploadedPart.push(i)
                    //计算百分比
                    let percentage = (fileOb.uploadedPart.length / fileOb.chunkNum).toFixed(2)
                    fileOb.uploadPercentage = percentage
                    self.postMessage({
                        type: "progress",
                        content: {
                            percentage: percentage
                        }
                    })
                    setUploadQueue()
                }
                
            })
        }
        reader.onerror = function (err) {
            fileOb.uploadStatus = "error"
            self.postMessage({
                type: "error",
                content: “****”
            })
        }
        //文件切片,并转换成包含一个 ArrayBuffer 对象以表示所读取文件的数据
        reader.readAsArrayBuffer(fileOb.file.slice(i * options.chunkSize, end))
    },
//并发队列
    setUploadQueue: function () {
        if (fileOb.uploadedPart.length === fileOb.chunkNum) {
            fileOb.uploadStatus = "success"
            self.postMessage({
                type: "success",
                content: {
                    fileOb: fileOb,
                }
            })
        } else {
            while (fileOb.uploadedPart.length < fileOb.chunkNum && fileOb.notUploadedPart.length > 0 && fileOb.runningPart.length < options.concurrency) {
                const index = fileOb.notUploadedPart.shift()
                uploadChunk(index)
                fileOb.runningPart.push(index)
            }
        }
    },

总结

大文件上传通常会有秒传这个要求,下章将解决这个问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值