在平常开发中常常会遇到文件上传的需求,vue-simple-uploader 就是一个基于 simple-uploader.js 和 Vue 结合作的一个上传组件,自带 UI,可覆盖、自定义;先来张动图看看效果:vue
其主要特色就是:git
支持文件、多文件、文件夹上传github
支持拖拽文件、文件夹上传npm
统一对待文件和文件夹,方便操做管理数组
可暂停、继续上传并发
错误处理app
支持“快传”,经过文件判断服务端是否已存在从而实现“快传”ide
上传队列管理,支持最大并发上传spa
分块上传eslint
支持进度、预估剩余时间、出错自动重试、重传等操做
经过npm
安装:npm install vue-simple-uploader --save
便可。
import Vue from 'vue' import uploader from 'vue-simple-uploader' import App from './App.vue' Vue.use(uploader) /* eslint-disable no-new */ new Vue({ render(createElement) { return createElement(App) } }).$mount('#app')
<template> <uploader :options="options" class="uploader-example"> <uploader-unsupport></uploader-unsupport> <uploader-drop> <p>Drop files here to upload or</p> <uploader-btn>select files</uploader-btn> <uploader-btn :attrs="attrs">select images</uploader-btn> <uploader-btn :directory="true">select folder</uploader-btn> </uploader-drop> <uploader-list></uploader-list> </uploader> </template> <script> export default { data () { return { options: { // 可经过 https://github.com/simple-uploader/Uploader/tree/develop/samples/Node.js 示例启动服务 target: '//localhost:3000/upload', testChunks: false }, attrs: { accept: 'image/*' } } } } </script> <style> .uploader-example { width: 880px; padding: 15px; margin: 40px auto 0; font-size: 12px; box-shadow: 0 0 10px rgba(0, 0, 0, .4); } .uploader-example .uploader-btn { margin-right: 4px; } .uploader-example .uploader-list { max-height: 440px; overflow: auto; overflow-x: hidden; overflow-y: auto; } </style>
上传根组件,可理解为一个上传器。
options {Object}
autoStart {Boolean}
默认 true
, 是否选择文件后自动开始上传。
upload-start
开始上传。
file-added(file)
添加了一个文件,通常用做文件校验,若是给 file
增长 ignored
属性为 true
的话就会被过滤掉。
file-removed(file)
移除一个文件(文件夹)。
files-submitted(files, fileList)
所选择的文件们添加到上传队列后触发。
files {Array}
纯文件列表,没有文件夹概念。
fileList {Array}
统一对待文件、文件夹列表。
started
是否开始上传了。
点选上传文件按钮。
directory {Boolean}
默认 false
, 是不是文件夹上传。
single {Boolean}
默认 false
, 若是设为 true
,则表明一次只能选择一个文件。
attrs {Object}
默认 {}
, 添加到 input 元素上的额外属性。
拖拽上传区域。
文件、文件夹列表,同等对待。
fileList {Array}
文件、文件夹组成数组。
文件列表,没有文件夹概念,纯文件列表。
files {Array}
文件列表。
不支持 HTML5 File API 的时候会显示。
文件、文件夹单个组件。
file {Uploader.File}
封装的文件实例。
list {Boolean}
若是是在 UploaderList
组件中使用的话,请设置为 true
。
file {Uploader.File}
文件实例。
list {Boolean}
是否在 UploaderList
组件中使用。
status {String}
当前状态,多是:success
, error
, uploading
, paused
, waiting
name {String}
文件名字。
paused {Boolean}
是否暂停了。
error {Boolean}
是否出错了。
averageSpeed {Number}
平均上传速度,单位字节每秒。
formatedAverageSpeed {String}
格式化后的平均上传速度,相似:3 KB / S
。
currentSpeed {Number}
当前上传速度,单位字节每秒。
isComplete {Boolean}
是否已经上传完成。
isUploading {Boolean}
是否在上传中。
size {Number}
文件或者文件夹大小。
formatedSize {Number}
格式化后文件或者文件夹大小,相似:10 KB
.
uploadedSize {Number}
已经上传大小,单位字节。
progress {Number}
介于 0 到 1 之间的小数,上传进度。
progressStyle {String}
进度样式,transform 属性,相似:{transform: '-50%'}
.
progressingClass {String}
正在上传中的时候值为:uploader-file-progressing
。
timeRemaining {Number}
预估剩余时间,单位秒。
formatedTimeRemaining {String}
格式化后剩余时间,相似:3 miniutes
.
type {String}
文件类型。
extension {String}
文件名后缀,小写。
fileCategory {String}
文件分类,其中之一:folder
, document
, video
, audio
, image
, unknown
。
地址:https://github.com/simple-uploader/vue-uploader。
欢迎使用、拍砖。