0.0.4 • Published 2 years ago

hs-multimode-upload v0.0.4

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

hs-multimode-upload

注意

Vue@2.4以上,Ant Design of Vue@ 1.5 <= 1.78

特性

  • 支持文件、多文件、文件夹上传

  • 支持拖拽文件、文件夹上传

  • 错误处理

  • 上传队列管理,支持最大并发上传

  • 分块上传

  • 出错自动重试

安装

npm install hs-multimode-upload  --save

使用

初始化

import Vue from 'vue'
import uploader from 'hs-multimode-upload'
import App from './App.vue'

Vue.use(uploader)

/* eslint-disable no-new */
new Vue({
  render(createElement) {
    return createElement(App)
  }
}).$mount('#app')

App.vue

<template>    
    自动上传
    <multimode-upload
            ref="upload"
            signatureUrl="https://省平台获取上传签名账号的地址"
            folder="test"
            tenantId="002"
            token="3a96313c-4fdf-4f95-bfae-e078a86b28a6"
            multiple
            type="oss"
            @finish="finish"
            list-type="picture-card"
            :remove="handleRemove"
            accept="*">
    
        <div v-if="fileList.length < 5">
            <a-icon type="plus" />
            <div class="ant-upload-text">
                Upload
            </div>
        </div>
    </multimode-upload>

    手动上传
    <multimode-upload
            ref="handleUpload"
            signatureUrl="https://省平台获取上传签名账号的地址"
            tenantId="002"
            token="3a96313c-4fdf-4f95-bfae-e078a86b28a6"
            multiple
            folder="test"
            type="minio"
            list-type="text"
            :remove="handleRemove"
            :before-upload="beforeUpload"
            accept="*">

        <div v-if="fileList.length < 5">
            <a-icon type="plus" />
            <div class="ant-upload-text">
                Upload
            </div>
        </div>
    </multimode-upload>

    <a-button
            type="primary"
            :disabled="fileList.length === 0"
            style="margin-top: 16px"
            @click="upload"
    >
        手动上传
    </a-button>

    拖拽上传
    <multimode-upload-dragger
            name="file"
            signatureUrl="https://省平台获取上传签名账号的地址"
            tenantId="002"
            token="3a96313c-4fdf-4f95-bfae-e078a86b28a6"
            folder="test"
            type="minio"
            :multiple="true"
    >
        <p class="ant-upload-drag-icon">
            <a-icon type="inbox" />
        </p>
        <p class="ant-upload-text">
            Click or drag file to this area to upload
        </p>
        <p class="ant-upload-hint">
            Support for a single or bulk upload. Strictly prohibit from uploading company data or other
            band files
        </p>
    </multimode-upload-dragger>


</template>

<script>
    export default {
        data () {
            return {
                fileList:[],
            }
        },
        methods: {
            handleRemove(file) {
                const index = this.fileList.indexOf(file);
                const newFileList = this.fileList.slice();
                newFileList.splice(index, 1);
                this.fileList = newFileList;
            },
            finish(data){
                console.log(data)
            },
            upload(){
                let fileList = this.$refs.handleUpload.fileList
                for(let i =0;i < this.fileList.length; i++){
                    setTimeout(()=>{
                        if((typeof(fileList[i].status) === 'undefined') || (fileList[i].status && fileList[i].status === 'error') ){
                            this.$refs.handleUpload.post(this.fileList[i])
                        }
                    }, 0)
                }
            },
            beforeUpload(file){
                this.fileList = [...this.fileList, file];
                return false;
            },
            handleChange(){},
        },
    }
</script>

API

参数说明类型默认值备注
folder上传的文件夹名称string必填,建议用项目名
tenantId租户IDstring必填
token当前用户的登录成功后的tokenstring必填
type上传类型stringossoss 或 minio
accept接受上传的文件类型, 详见 input accept Attributestring
action上传的地址string|(file) => Promise
signatureUrl获取上传签名账号的地址stringhttps://测试服获取上传签名账号的地址
method上传请求的 http methodstring'post'
directory支持上传文件夹(caniusebooleanfalse
beforeUpload上传文件之前的钩子,参数为上传的文件,若返回 false 则停止上传。支持返回一个 Promise 对象,Promise 对象 reject 时则停止上传,resolve 时开始上传( resolve 传入 FileBlob 对象则上传 resolve 传入对象)。注意:IE9 不支持该方法(file, fileList) => boolean | Promise
customRequest本插件已内部使用该方法覆盖默认的上传行为,建议不要再次覆盖Function
data上传所需参数或返回上传参数的方法object|(file) => object
defaultFileList默认已经上传的文件列表object[]
disabled是否禁用booleanfalse
fileList已经上传的文件列表(受控)object[]
headers设置上传的请求头部,IE10 以上有效object
listType上传列表的内建样式,支持三种基本样式 text, picturepicture-cardstring'text'
multiple是否支持多选文件,ie10+ 支持。开启后按住 ctrl 可选择多个文件。booleanfalse
name发到后台的文件参数名string'file'
previewFile自定义文件预览逻辑(file: File | Blob) => Promise<dataURL: string>
showUploadList是否展示 uploadList, 可设为一个对象,用于单独设定 showPreviewIcon 和 showRemoveIconBoolean or { showPreviewIcon?: boolean, showRemoveIcon?: boolean }true
supportServerRender服务端渲染时需要打开这个booleanfalse
withCredentials上传请求时是否携带 cookiebooleanfalse
openFileDialogOnClick点击打开文件对话框booleantrue
remove  点击移除文件时的回调,返回值为 false 时不移除。支持返回一个 Promise 对象,Promise 对象 resolve(false) 或 reject 时不移除。              Function(file): boolean | Promise 
transformFile  在上传之前转换文件。支持返回一个 Promise 对象  Function(file): string \| Blob \| File \| Promise<string \| Blob \| File>无  

事件

事件名称说明回调参数备注
change上传文件改变时的状态,本插件已使用该方法进行数据处理。Function
finish文件上传完的回调(不管成功失败都会触发)Function
preview点击文件链接或预览图标时的回调Function(file)
download点击下载文件时的回调,如果没有指定,则默认跳转到文件 url 对应的标签页。Function(file): void | 跳转新标签页
reject拖拽文件不符合 accept 类型时的回调Function(fileList)

change

上传中、完成、失败都会调用这个函数。

文件状态改变的回调,返回为:

{
  file: { /* ... */ },
  fileList: [ /* ... */ ],
  event: { /* ... */ },
}
  1. file 当前操作的文件对象。

    {
       uid: 'uid',      // 文件唯一标识,建议设置为负数,防止和内部产生的 id 冲突
       name: 'xx.png',   // 文件名
       status: 'done', // 状态有:uploading done error removed
       response: '{"status": "success"}', // 服务端响应内容
       linkProps: '{"download": "image"}', // 下载链接额外的 HTML 属性
       xhr: 'XMLHttpRequest{ ... }', // XMLHttpRequest Header
    }
  2. fileList 当前的文件列表。

  3. event 上传中的服务端响应内容,包含了上传进度等信息,高级浏览器支持。