修改文件上传组件

This commit is contained in:
jmal 2020-02-20 17:54:06 +08:00
parent a083a0ab47
commit 3dc510409a
2 changed files with 91 additions and 51 deletions

View File

@ -5,7 +5,8 @@
<uploader <uploader
ref="uploader" ref="uploader"
:options="options" :options="options"
:auto-start="false" :file-status-text="statusText"
:auto-start="true"
class="uploader-app" class="uploader-app"
@files-added="onFilesAdded" @files-added="onFilesAdded"
@file-success="onFileSuccess" @file-success="onFileSuccess"
@ -67,9 +68,9 @@ export default {
username: this.$store.state.user.name, username: this.$store.state.user.name,
options: { options: {
target: api.simpleUploadURL, target: api.simpleUploadURL,
chunkSize: 2 * 1024 * 1024, chunkSize: 1024 * 1024,
maxChunkRetries: 1, // 最大重试次数 maxChunkRetries: 1, // 最大重试次数
simultaneousUploads: 3, // 并发上传数 simultaneousUploads: 1, // 并发上传数
testChunks: true, // 是否开启服务器分片校验 testChunks: true, // 是否开启服务器分片校验
// 服务器分片校验函数,秒传及断点续传基础 // 服务器分片校验函数,秒传及断点续传基础
checkChunkUploadedByResponse: function(chunk, message) { checkChunkUploadedByResponse: function(chunk, message) {
@ -82,6 +83,14 @@ export default {
// 断点续传 // 断点续传
return (res.resume || []).indexOf(chunk.offset + 1) >= 0 return (res.resume || []).indexOf(chunk.offset + 1) >= 0
}, },
parseTimeRemaining: function (timeRemaining, parsedTimeRemaining) {
return parsedTimeRemaining
.replace(/\syears?/, '年')
.replace(/\days?/, '天')
.replace(/\shours?/, '小时')
.replace(/\sminutes?/, '分钟')
.replace(/\sseconds?/, '秒')
},
headers: { headers: {
'jmal-token': $this.$store.state.user.token 'jmal-token': $this.$store.state.user.token
}, },
@ -91,6 +100,13 @@ export default {
// accept: ACCEPT_CONFIG.getAll() // accept: ACCEPT_CONFIG.getAll()
accept: '*' accept: '*'
}, },
statusText: {
success: '成功了',
error: '出错了',
uploading: '上传中',
paused: '暂停中',
waiting: '等待中'
},
panelShow: false, // 选择文件后,展示上传panel panelShow: false, // 选择文件后,展示上传panel
collapse: false collapse: false
} }
@ -115,6 +131,10 @@ export default {
$('#folder-uploader-btn').click() $('#folder-uploader-btn').click()
} }
}) })
this.$nextTick(() => {
window.uploader = this.$refs.uploader.uploader
})
}, },
destroyed() { destroyed() {
Bus.$off('openUploader') Bus.$off('openUploader')
@ -154,23 +174,29 @@ export default {
this.panelShow = true this.panelShow = true
console.log(pathsLength) console.log(pathsLength)
// this.computeMD5(file) // this.computeMD5(file)
if (pathsLength < 1) { // if (pathsLength < 1) {
this.computeMD5(file) // // this.computeMD5(file)
} else { // } else {
console.log(pathsLength) // console.log(pathsLength)
Object.assign(this.uploader.opts, { // Object.assign(this.uploader.opts, {
query: { // query: {
isFolder: false, // isFolder: false,
...this.params // ...this.params
} // }
}) // })
file.resume() // this.uploader.resume()
} // }
Object.assign(this.uploader.opts, {
query: {
isFolder: false,
...this.params
}
})
Bus.$emit('fileAdded') Bus.$emit('fileAdded')
}) })
}, },
onFileProgress(rootFile, file, chunk) { onFileProgress(rootFile, file, chunk) {
// console.log(`上传中 ${file.name},chunk:${chunk.startByte / 1024 / 1024} ~ ${chunk.endByte / 1024 / 1024}`) console.log(`上传中 ${file.name},chunk:${chunk.startByte / 1024 / 1024} ~ ${chunk.endByte / 1024 / 1024}`)
}, },
onFileSuccess(rootFile, file, response) { onFileSuccess(rootFile, file, response) {
const res = JSON.parse(response) const res = JSON.parse(response)
@ -182,6 +208,7 @@ export default {
this.statusSet(file.id, 'failed') this.statusSet(file.id, 'failed')
return return
} else { } else {
console.log(`上传成功 ${file.name}`)
this.statusSet(file.id, 'success') this.statusSet(file.id, 'success')
} }
// 如果服务端返回需要合并 // 如果服务端返回需要合并

View File

@ -1,45 +1,58 @@
<!-- 一个上传事例文件 -->
<template> <template>
<div> <uploader :options="options" :file-status-text="statusText" class="uploader-example" ref="uploader" @file-complete="fileComplete" @complete="complete"></uploader>
<el-button @click="upload">上传</el-button>
</div>
</template> </template>
<script> <script>
import Bus from '@/assets/js/bus' export default {
data () {
export default { return {
components: {}, options: {
data() { target: '//localhost:3000/upload', // '//jsonplaceholder.typicode.com/posts/',
return {} testChunks: false
}, },
computed: {}, attrs: {
mounted() { accept: 'image/*'
// 文件选择后的回调 },
Bus.$on('fileAdded', () => { statusText: {
console.log('文件已选择') success: '成功了',
}) error: '出错了',
uploading: '上传中',
// 文件上传成功的回调 paused: '暂停中',
Bus.$on('fileSuccess', () => { waiting: '等待中'
console.log('文件上传成功') }
}) }
}, },
destroyed() { methods: {
Bus.$off('fileAdded') complete () {
Bus.$off('fileSuccess') console.log('complete', arguments)
}, },
methods: { fileComplete () {
upload() { console.log('file complete', arguments)
// 打开文件选择框 }
Bus.$emit('openUploader', { },
id: '1111' // 传入的参数 mounted () {
this.$nextTick(() => {
window.uploader = this.$refs.uploader.uploader
}) })
} }
} }
}
</script> </script>
<style scoped lang="scss"> <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> </style>