0.0.14 • Published 9 months ago

put-file-tools v0.0.14

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

put-file-tools 适用于vue3的大文件上传插件

序言

put-file-tools 支持大文件上传,断点续传,实时获取上传状态

属性

参数说明类型可选值默认值
chunkSize切片大小number10 1024 1024
inspectRequest 校验文件是否在服务器中存在,已上传返回true,未上传返回false,上传部分切片返回切片hash数组,数组示例:'537746f9cfb594a7','69898a563de05a1a'function(hash)
uploadApiUrl上传文件的接口地址string
concurrencyNumber切片上传的并发数number3

方法

事件名说明回调参数
onChange文件状态改变时的钩子,添加文件会被调用function(file)
onUploadProgress返回文件上传进度function(progress)
uploadError上传失败function(error)
onFinish上传完成function(hash)

安装使用

$ npm install put-file-tools
$ yarn add put-file-tools
<script setup>
import { ref } from 'vue'
import PutFileTools from 'PutFileTools'
import axios from 'axios'

let fileProgress = ref(0)
const inspectApiUrl = '/api/uploadChunk/inspect'
const uploadApiUrl = '/api/uploadChunk'
const uploadFinishApiUrl = '/api/uploadChunk/finish'

// 检查文件是否存在服务器中
async function inspectRequest(hash, file) {
  const res = await axios({
    method: 'POST',
    url: `${inspectApiUrl}/${hash}`,
    data: { hash, name: file.name, size: file.size, type: file.type },
  }).catch((error) => {
    console.log('inspectRequest  error:', error)
  })
  return res.data.data
}

// 上传进度
function onUploadProgress(progress) {
  fileProgress.value = progress
  console.log('onUploadProgress  progress', progress)
}

function onChange(file) {
  // console.log('onChange  file',file)
}

// 文件切片全部上传完成
async function onFinish(hash) {
  const res = await axios({
    method: 'GET',
    url: `${uploadFinishApiUrl}/${hash}`,
  }).catch((error) => {
    console.log('inspectRequest  error:', error)
  })
}
function uploadError(err) {
  console.log('uploadError err',err);
}
</script>

<template>
  <main>
    <PutFileTools
      :chunkSize="1024 * 1024 * 100"
      :uploadApiUrl="uploadApiUrl"
      :inspectRequest="inspectRequest"
      @onUploadProgress="onUploadProgress"
      @onChange="onChange"
      @onFinish="onFinish"
      @uploadError="uploadError"
    />
  </main>
</template>
0.0.14

9 months ago

0.0.13

9 months ago

0.0.12

9 months ago

0.0.11

9 months ago

0.0.10

9 months ago

0.0.9

9 months ago

0.0.8

10 months ago

0.0.7

10 months ago

0.0.6

10 months ago

0.0.5

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago