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

@prequest/uploader

v1.5.2

Published

针对多文件、大文件上传提供了一种便捷的方法,来控制同时进行的任务数量、和文件的切片上传。

Downloads

9

Readme

@prequest/uploader

针对多文件、大文件上传提供了一种便捷的方法,来控制同时进行的任务数量、和文件的切片上传。

安装

npm install @prequest/uploader

使用

下面演示了一个 60M 的文件,切片成了 6 份,进行 6 次 HTTP 请求,其中队列中同时上传的任务有 3 个。

import Uploader from '@prequest/uploader'
import { prequest } from '@prequest/xhr'

const Upload = () => {
  const [fileList, setFileList] = useState<File[]>([])
  const uploader = useRef<Uploader | null>(null)

  useEffect(() => {
    uploader.current = new Uploader({
      chipSize: 10 * 1024 * 1024, // 切片大小,10M
      poolLimit: 3, // 同时进行的任务数量
      customFormData(formData, { idx, file, chunk }) {
        // 要上传数据的 formData
        formData.append('name', file.name)
        formData.append('index', `${idx}`)
        formData.append('chunk', chunk)
        return formData
      },
      request(formData, { idx, file, chunk }) {
        // 上传过程
        return prequest.post('/upload', {
          data: formData,
          onUploadProgress(e) {
            console.log(`${file.name} 的第${idx}个分片,已上传:${e.loaded}`)
          },
        })
      },
    })
  }, [])

  async function onSubmit() {
    try {
      // 上传任务
      await uploader.current.upload(fileList)
    } catch (e) {
      console.log('上传文件失败')
    }
  }

  return (
    <div>
      <input type="file" onChange={e => setFileList(e.target.files)} />
      <button onClick={onSubmit}>上传</button>
    </div>
  )
}

参数

配置参数

| 名称 | 类型 | 默认 | 必填 | 含义 | | -------------- | ---------------------------------------------------------------- | ------------------------------------------------------ | ---- | --------------------- | | chipSize | number | 10 * 1024 * 1024 | 否 | 切片大小 | | poolLimit | number | 3 | 否 | 同时上传的任务数量 | | customFormData | (formData: FormData, opt: CallbackOption) => FormData | 默认会将 name, index, chunk 三个字段添加到 formData 中 | 否 | 定义要上传的 formData | | request | (formData: FormData, opt: CallbackOption) => Promise<Response> | | 是 | 定义要上传的请求方法 |

CallbackOption

| 名称 | 类型 | 含义 | | ----- | ------ | ---------- | | idx | number | 切片的索引 | | chunk | Blob | 切片文件 | | file | File | 原始文件 |