# hs-multimode-upload

> 聚合上传组件

Latest version **0.0.4** (published 2022-05-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install hs-multimode-upload
pnpm add hs-multimode-upload
yarn add hs-multimode-upload
bun add hs-multimode-upload
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; large bundle; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2022-05-16 |
| First published | 2022-05-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 14.5 MB |
| Known vulnerabilities | 0 (+3 in 3 direct dependencies) |
| Install scripts | no |
| Author | HY |
| Maintainers | zhukun |

## Links

- npm: https://www.npmjs.com/package/hs-multimode-upload
- npm.io page: https://npm.io/package/hs-multimode-upload

## Dependencies (8)

- [vue](https://npm.io/package/vue.md) ^2.6.14
- [minio](https://npm.io/package/minio.md) ^7.0.28
- [stream](https://npm.io/package/stream.md) ^0.0.2
- [ali-oss](https://npm.io/package/ali-oss.md) ^6.17.1
- [aws-sdk](https://npm.io/package/aws-sdk.md) ^2.1133.0
- [ant-design-vue](https://npm.io/package/ant-design-vue.md) ^1.7.8
- [babel-plugin-import](https://npm.io/package/babel-plugin-import.md) ^1.13.5
- [vue-template-compiler](https://npm.io/package/vue-template-compiler.md) ^2.6.14

## Recent versions

- 0.0.4 (latest) — 2022-05-16
- 0.0.3 — 2022-05-13
- 0.0.2 — 2022-05-13
- 0.0.1 — 2022-05-13

## README

# hs-multimode-upload


## 注意
Vue@2.4以上，Ant Design of Vue@ 1.5 <= 1.78

## 特性

- [x] 支持文件、多文件、文件夹上传

- [x] 支持拖拽文件、文件夹上传

- [x] 错误处理

- [x] 上传队列管理，支持最大并发上传

- [x] 分块上传

- [x] 出错自动重试

## 安装

``` bash
npm install hs-multimode-upload  --save
```

## 使用

### 初始化

``` js
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

``` 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 | 租户ID | string | 无 | 必填 |
| token | 当前用户的登录成功后的token | string | 无 | 必填 |
| type | 上传类型 | string | oss | oss 或 minio |
| accept | 接受上传的文件类型, 详见 [input accept Attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#accept) | string | 无 |  |
| action | 上传的地址 | string\|(file) => `Promise` | 无 |  |
| signatureUrl | 获取上传签名账号的地址 | string | https://测试服获取上传签名账号的地址 |  |
| method | 上传请求的 http method | string | 'post' |  |
| directory | 支持上传文件夹（[caniuse](https://caniuse.com/#feat=input-file-directory)） | boolean | false |  |
| beforeUpload | 上传文件之前的钩子，参数为上传的文件，若返回 `false` 则停止上传。支持返回一个 Promise 对象，Promise 对象 reject 时则停止上传，resolve 时开始上传（ resolve 传入 `File` 或 `Blob` 对象则上传 resolve 传入对象）。**注意：IE9 不支持该方法**。 | (file, fileList) => `boolean | Promise` |  |  |
| customRequest | 本插件已内部使用该方法覆盖默认的上传行为，建议不要再次覆盖 | Function | 无 |  |
| data | 上传所需参数或返回上传参数的方法 | object\|(file) => object | 无 |  |
| defaultFileList | 默认已经上传的文件列表 | object\[] | 无 |  |
| disabled | 是否禁用 | boolean | false |  |
| fileList | 已经上传的文件列表（受控） | object\[] | 无 |  |
| headers | 设置上传的请求头部，IE10 以上有效 | object | 无 |  |
| listType | 上传列表的内建样式，支持三种基本样式 `text`, `picture` 和 `picture-card` | string | 'text' |  |
| multiple | 是否支持多选文件，`ie10+` 支持。开启后按住 ctrl 可选择多个文件。 | boolean | false |  |
| name | 发到后台的文件参数名 | string | 'file' |  |
| previewFile | 自定义文件预览逻辑 | (file: File \| Blob) => Promise<dataURL: string> | 无 |  |
| showUploadList | 是否展示 uploadList, 可设为一个对象，用于单独设定 showPreviewIcon 和 showRemoveIcon | Boolean or { showPreviewIcon?: boolean, showRemoveIcon?: boolean } | true |  |
| supportServerRender | 服务端渲染时需要打开这个 | boolean | false |  |
| withCredentials | 上传请求时是否携带 cookie | boolean | false |  |
| openFileDialogOnClick | 点击打开文件对话框 | boolean | true |  |
| 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

> 上传中、完成、失败都会调用这个函数。

文件状态改变的回调，返回为：

```js
{
  file: { /* ... */ },
  fileList: [ /* ... */ ],
  event: { /* ... */ },
}
```

1. `file` 当前操作的文件对象。

   ```js
   {
      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` 上传中的服务端响应内容，包含了上传进度等信息，高级浏览器支持。

---
_Source: https://npm.io/package/hs-multimode-upload · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
