0.0.4 • Published 2 years ago
hs-multimode-upload v0.0.4
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 | 租户ID | string | 无 | 必填 |
token | 当前用户的登录成功后的token | string | 无 | 必填 |
type | 上传类型 | string | oss | oss 或 minio |
accept | 接受上传的文件类型, 详见 input accept Attribute | string | 无 | |
action | 上传的地址 | string|(file) => Promise | 无 | |
signatureUrl | 获取上传签名账号的地址 | string | https://测试服获取上传签名账号的地址 | |
method | 上传请求的 http method | string | 'post' | |
directory | 支持上传文件夹(caniuse) | 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
上传中、完成、失败都会调用这个函数。
文件状态改变的回调,返回为:
{
file: { /* ... */ },
fileList: [ /* ... */ ],
event: { /* ... */ },
}
file
当前操作的文件对象。{ uid: 'uid', // 文件唯一标识,建议设置为负数,防止和内部产生的 id 冲突 name: 'xx.png', // 文件名 status: 'done', // 状态有:uploading done error removed response: '{"status": "success"}', // 服务端响应内容 linkProps: '{"download": "image"}', // 下载链接额外的 HTML 属性 xhr: 'XMLHttpRequest{ ... }', // XMLHttpRequest Header }
fileList
当前的文件列表。event
上传中的服务端响应内容,包含了上传进度等信息,高级浏览器支持。