Merge pull request #130 from jamebal/upload

fix: 修复上传大量文件传输进度条卡住的问题
This commit is contained in:
jmal 2024-05-28 17:59:39 +08:00 committed by GitHub
commit 58fcada8af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 54 additions and 30 deletions

View File

@ -16,11 +16,11 @@ export default {
}) })
}, },
// 检查文件是否存在 // 检查文件是否存在
checkExist: function (params) { checkExist: function (data) {
return request({ return request({
url: "checkExist", url: "checkExist",
method: "post", method: "post",
params data
}) })
}, },
// 上传文件夹 // 上传文件夹

View File

@ -17,7 +17,7 @@
> >
<uploader-unsupport></uploader-unsupport> <uploader-unsupport></uploader-unsupport>
<uploader-drop v-if="dragover && enableDragUplaod" class="uploader-drop"> <uploader-drop v-if="dragover && enableDragUpload" class="uploader-drop">
<span>上传文件到当前目录下</span> <span>上传文件到当前目录下</span>
</uploader-drop> </uploader-drop>
@ -112,7 +112,6 @@
<script> <script>
// import { ACCEPT_CONFIG } from '@/assets/js/config'
import store from '@/store' import store from '@/store'
import $ from 'jquery' import $ from 'jquery'
import SparkMD5 from 'spark-md5' import SparkMD5 from 'spark-md5'
@ -133,7 +132,7 @@ export default {
// speedSmoothingFactor: 0.1, // speedSmoothingFactor: 0.1,
// progressCallbacksInterval: 500, // progressCallbacksInterval: 500,
maxChunkRetries: 3, // 最大重试次数 maxChunkRetries: 3, // 最大重试次数
simultaneousUploads: 5, // 并发上传数 simultaneousUploads: 3, // 并发上传数
testChunks: true, // 是否开启服务器分片校验 testChunks: true, // 是否开启服务器分片校验
// 服务器分片校验函数,秒传及断点续传基础 // 服务器分片校验函数,秒传及断点续传基础
checkChunkUploadedByResponse: function (chunk, message) { checkChunkUploadedByResponse: function (chunk, message) {
@ -190,7 +189,7 @@ export default {
fileListScrollTop: 0, fileListScrollTop: 0,
dragoverLoop: null, dragoverLoop: null,
successMsg: null, successMsg: null,
enableDragUplaod: false,// 是否启用拖拽上传 enableDragUpload: false,// 是否启用拖拽上传
uploader: null, uploader: null,
} }
}, },
@ -200,7 +199,7 @@ export default {
watch: { watch: {
$route(route) { $route(route) {
// 只有首页才启用拖拽上传 // 只有首页才启用拖拽上传
this.enableDragUplaod = route.path === '/' this.enableDragUpload = route.path === '/'
}, },
message(msg) { message(msg) {
switch (msg.event) { switch (msg.event) {
@ -230,16 +229,27 @@ export default {
$('#folder-uploader-btn').click() $('#folder-uploader-btn').click()
} }
break break
case 'request':
if (msg.data === 0) {
if (this.uploader.fileList.length === 0 && this.panelShow) {
this.fileListLength = this.uploader.fileList.length
this.process = 100
this.setPageTitle()
this.uploader.cancel()
this.displayPanel(false)
}
}
break
} }
} }
}, },
mounted() { mounted() {
this.enableDragUplaod = this.$route.path === '/' this.enableDragUpload = this.$route.path === '/'
let that = this let that = this
let dropbox = document.body let dropbox = document.body
document.body.ondragstart = function (e) { document.body.ondragstart = function (e) {
if (that.enableDragUplaod) { if (that.enableDragUpload) {
if (e.target.slot === 'jmal') { if (e.target.slot === 'jmal') {
that.isDragStart = true that.isDragStart = true
} }
@ -357,6 +367,9 @@ export default {
this.doUploadBefore(files) this.doUploadBefore(files)
} }
}, },
displayPanel(display) {
this.panelShow = display
},
doUploadBefore(files) { doUploadBefore(files) {
this.fileListLength = this.uploader.fileList.length this.fileListLength = this.uploader.fileList.length
const filePaths = this.uploader.filePaths const filePaths = this.uploader.filePaths
@ -374,15 +387,12 @@ export default {
currentDirectory: encodeIfNeeded(this.params.currentDirectory), currentDirectory: encodeIfNeeded(this.params.currentDirectory),
username: this.params.username, username: this.params.username,
userId: this.params.userId userId: this.params.userId
}).then(() => {
this.getFileList()
}).catch(e => {
}) })
}) })
} }
if (window.pc) { if (window.pc) {
this.pc = true this.pc = true
this.panelShow = true this.displayPanel(true)
} else { } else {
this.pc = false this.pc = false
this.shrink() this.shrink()
@ -400,6 +410,13 @@ export default {
this.uploader.resume() this.uploader.resume()
}) })
}, },
setPageTitle() {
if (this.process === -10 || this.process === 100 || this.fileListLength === 0) {
document.title = `${this.$route.meta.title}`
} else {
document.title = `${this.process}% | ${this.$route.meta.title}`
}
},
onFileProgress(rootFile, file, chunk) { onFileProgress(rootFile, file, chunk) {
this.netSpeed = formatNetSpeed(file.currentSpeed) this.netSpeed = formatNetSpeed(file.currentSpeed)
this.process = Math.trunc(window.uploader.progress() * 100) this.process = Math.trunc(window.uploader.progress() * 100)
@ -490,7 +507,7 @@ export default {
} }
if (this.process === -10 || this.process === 100 || this.fileListLength === 0) { if (this.process === -10 || this.process === 100 || this.fileListLength === 0) {
this.uploader.cancel() this.uploader.cancel()
this.panelShow = false this.displayPanel(false)
} }
}, },
onFileError(rootFile, file, response) { onFileError(rootFile, file, response) {
@ -641,7 +658,7 @@ export default {
close() { close() {
if (this.process === -10 || this.process === 100 || this.fileListLength === 0) { if (this.process === -10 || this.process === 100 || this.fileListLength === 0) {
this.uploader.cancel() this.uploader.cancel()
this.panelShow = false this.displayPanel(false)
} else { } else {
this.$confirm('还有文件正在上传, 确定要关闭吗?', '提示', { this.$confirm('还有文件正在上传, 确定要关闭吗?', '提示', {
confirmButtonText: '确定', confirmButtonText: '确定',
@ -649,7 +666,7 @@ export default {
type: 'warning' type: 'warning'
}).then(() => { }).then(() => {
this.uploader.cancel() this.uploader.cancel()
this.panelShow = false this.displayPanel(false)
}) })
} }
}, },

View File

@ -1,5 +1,6 @@
// import { MessageBox, Message } from 'element-ui' // import { MessageBox, Message } from 'element-ui'
import store from '@/store' import store from '@/store'
import _ from "lodash";
import {getShareId, getShareToken, getToken, getUsername} from '@/utils/auth' import {getShareId, getShareToken, getToken, getUsername} from '@/utils/auth'
// create an axios instance // create an axios instance
@ -12,6 +13,7 @@ const service = axios.create({
// request interceptor // request interceptor
service.interceptors.request.use( service.interceptors.request.use(
config => { config => {
store.dispatch('updateMessage', {event: 'request', data: 1})
// do something before request is sent // do something before request is sent
if (store.getters.token) { if (store.getters.token) {
config.headers['jmal-token'] = getToken() config.headers['jmal-token'] = getToken()
@ -31,12 +33,24 @@ service.interceptors.request.use(
} }
) )
const responseErr = _.throttle((error) => {
let msg = '哎呀!服务器出错了,请稍后再试!'
if (error.response && error.response.status === 403) {
msg = '您的请求被服务器拒绝!'
}
Vue.prototype.$message({
message: msg,
type: 'error',
duration: 5 * 1000
})
}, 3000)
// response interceptor // response interceptor
service.interceptors.response.use( service.interceptors.response.use(
/** /**
* If you want to get http information such as headers or status * If you want to get http information such as headers or status
* Please return response => response * Please return response => response
*/ */
/** /**
* Determine the request status by custom code * Determine the request status by custom code
@ -44,6 +58,7 @@ service.interceptors.response.use(
* You can also judge the status by HTTP Status Code * You can also judge the status by HTTP Status Code
*/ */
response => { response => {
store.dispatch('updateMessage', {event: 'request', data: 0})
const res = response.data const res = response.data
if (res.code !== 0) { if (res.code !== 0) {
if (res.code === -2) { if (res.code === -2) {
@ -56,7 +71,7 @@ service.interceptors.response.use(
// to re-login // to re-login
setTimeout(function () { setTimeout(function () {
const hasToken = getToken() const hasToken = getToken()
if(hasToken){ if (hasToken) {
Vue.prototype.$confirm('登录已失效,请重新登录', '确认登出', { Vue.prototype.$confirm('登录已失效,请重新登录', '确认登出', {
confirmButtonText: '重新登录', confirmButtonText: '重新登录',
cancelButtonText: '取消', cancelButtonText: '取消',
@ -67,7 +82,7 @@ service.interceptors.response.use(
}) })
}) })
} }
},200) }, 200)
} else { } else {
Vue.prototype.$message({ Vue.prototype.$message({
message: res.message || '服务器开小差了...', message: res.message || '服务器开小差了...',
@ -78,10 +93,10 @@ service.interceptors.response.use(
return Promise.reject() return Promise.reject()
} else { } else {
let message = '' let message = ''
if(res.message){ if (res.message) {
message = res.message.toString() message = res.message.toString()
} }
if(message !== 'true'){ if (message !== 'true') {
Vue.prototype.$message({ Vue.prototype.$message({
message: res.message, message: res.message,
type: 'success', type: 'success',
@ -92,15 +107,7 @@ service.interceptors.response.use(
} }
}, },
error => { error => {
let msg = '哎呀!服务器出错了,请稍后再试!' responseErr(error)
if (error.response && error.response.status === 403) {
msg = '您的请求被服务器拒绝!'
}
Vue.prototype.$message({
message: msg,
type: 'error',
duration: 5 * 1000
})
return Promise.reject(error) return Promise.reject(error)
} }
) )