npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

file-slicing-processor

v1.2.1

Published

Web-side file slicing upload processor

Downloads

2

Readme

file-slicing-processor

场景


主要用来处理web端文件分片上传过程中文件分片的过程,提供文件切片,进度条(上传进度、文件md5进度),输出文件碎片等功能(支持TS)

通过以上功能可实现文件分片上传、断点续传、秒传等文件上传操作

安装


npm install file-slicing-processor

快速上手


  1. 简单文件分片上传业务场景
  import FileSlicingProcessor from 'file-slicing-processor'

  const file = document.querySelector('#file').files[0]
  let done = false

  const processor = new FileSlicingProcessor(file, {
    chunkSize: 100 * 1024 * 1024,
    onProgress (progress) {
      console.log(`uplaod progress is ${progress}`)
    },
    onFinished (fileInfo) {
      console.log(`the file upload done, the file info is `, fileInfo)
      done = true
    },
    onError (err) {
      console.error(`file upload error, err: `, err)
    },
    httpRequest (params) {
      // 自定义请求必须返回一个Promise结果
      return axios.post(`/file/upload`, params)
    }
  });
  1. 大文件秒传和断点续传业务场景
import FileSlicingPorcessor from 'file-slicing-processor'

const file = document.querySelector('#file').files[0]
let done = false

const processor = new FileSlicingProcessor(file, {
 onProgress (progress) {
   console.log(`uplaod progress is ${progress}`)
 },
 onFinished (fileInfo) {
   console.log(`the file upload done, the file info is`, fileInfo)
   done = true
 },
 onError (err) {
   console.error(`file upload error, err: `, err)
 },
 onFileMD5Progress (progress) {
   console.log(`file md5 progress is ${progress}`)
 },
 httpRequest (params) {
   return axios.post(`/file/upload`, params)
 }
})

processor.getFileRealMD5().then(async md5 => {
 console.log(`file real md5 is ${md5}`)
 // 若服务端存在此文件,则实现秒传
 const serverMD5 = await axios.post(`/query/file/info`, {
   fileName: file.name,
   fileSize: file.size,
   md5
 })
 if (serverMD5 === md5) {
   done = true
   processor.done()
 }
})

// 断点续传,当已上传3个碎片时
const fileInfo = processor.getFileInfo()
processor.start(fileInfo.uploadedChunks)
// 暂停
processor.pause()
// 重启
processor.play()

API

  1. 属性

| 名称 | 说明 | 类型 | 参数 | | :--- | :---- | :---- | :---- | | chunkSize | 碎片大小 | Number | 无 | | uploadedChunkNum | 已上传碎片数 | Number | 无 | | chunkMD5 | 碎片MD5 | Boolean | 默认为 false | | retryCount | 重试次数 | Number | 默认值为3次 | | httpRequest | 自定义请求方法 | Function | params: HttpRequestParams | | onProgress | 上传进度回调 | Function | progress?: Number 进度(百分比数值) | | onFinished | 文件分片完成回调 | Function | params: { fileInfo: FileInfo } | | onFileMD5Progress | 文件MD5进度回调 | Function | progress?: Number 进度(百分比数值)

  1. 方法

| 名称 | 说明 | 参数 | 返回值 | | :-- | :-- | :-- | :-- | | getFileInfo | 获取文件信息 | 无 | FileInfo | getFileRealMD5 | 异步获取文件精准MD5 | 无 | Promise.then() / Promise.catch() | | getChunk | 获取当前文件碎片 | 无 | Blob | | getChunkMD5 | 获取当前碎片MD5 | 无 | Promise.then() / Promise.catch() | | start | 文件分片开始 | num: 文件碎片索引,代表从第几片开始切片 | 无 | | pause | 文件分片暂停 | 无 | 无 | | play | 文件分片重启 | 无 | 无 | | done | 文件分片完成 | 无 | 无 | | next | 文件分片下一次操作指令方法 | 无 | 无 |

  1. 类型

| 接口 | 属性 | | :-- | :-- | | FileInfo | id: Number (当前文件唯一标识) name: String(文件名) size: Number(文件size) totalChunks: Number(文件碎片总数) md5: String(文件md5) uploadedChunks: Number(已上传碎片数) } | | HttpRequestParams | chunk: Blob(文件碎片) chunkNum: Number(碎片索引) chunkMD5?: String (碎片MD5,chunkMD5true 时返回) 方法必须返回一个 promise 的结果

说明


这个工具库主要是对文件进行分片操作,不参与文件碎片的上传过程,这样可以让用户灵活的进行二次开发,工具库会提供一系列的回调函数来反馈文件分片过程中的进度和状态

注意事项


  • 进度条是通过碎片索引/总碎片数得到的,所以需要细粒度的进度参数可以通过设置更小的分片阈值实现
  • 文件 md5 默认为name、size、lastModified连接的字符串,如果需要准确的md5,可以使用getFileRealMD5方法来异步获取,并提供md5过程的进度回调函数
  • 分片阈值默认为50MB