|
|
|
@ -15,8 +15,10 @@
|
|
|
|
|
:auto-upload="false" |
|
|
|
|
ref="uploadRef" |
|
|
|
|
:on-change="fileOnChange" |
|
|
|
|
:limit="1" |
|
|
|
|
:on-exceed="handleExceed" |
|
|
|
|
:on-success="hadleSuccess" |
|
|
|
|
show-file-list |
|
|
|
|
multiple |
|
|
|
|
> |
|
|
|
|
<el-icon class="el-icon--upload"><upload-filled /></el-icon> |
|
|
|
|
<div class="el-upload__text"> |
|
|
|
@ -24,7 +26,8 @@
|
|
|
|
|
<em>{{ $t('database.clickHelper') }}</em> |
|
|
|
|
</div> |
|
|
|
|
<template #tip> |
|
|
|
|
<el-progress v-if="loading" text-inside :stroke-width="12" :percentage="uploadPrecent"></el-progress> |
|
|
|
|
<el-text>{{ uploadHelper }}</el-text> |
|
|
|
|
<el-progress v-if="loading" text-inside :stroke-width="20" :percentage="uploadPrecent"></el-progress> |
|
|
|
|
</template> |
|
|
|
|
</el-upload> |
|
|
|
|
<template #footer> |
|
|
|
@ -41,7 +44,7 @@
|
|
|
|
|
<script setup lang="ts"> |
|
|
|
|
import { ref } from 'vue'; |
|
|
|
|
import { UploadFile, UploadFiles, UploadInstance, UploadProps, UploadRawFile } from 'element-plus'; |
|
|
|
|
import { ChunkUploadFileData } from '@/api/modules/files'; |
|
|
|
|
import { ChunkUploadFileData, UploadFileData } from '@/api/modules/files'; |
|
|
|
|
import i18n from '@/lang'; |
|
|
|
|
import DrawerHeader from '@/components/drawer-header/index.vue'; |
|
|
|
|
import { MsgSuccess } from '@/utils/message'; |
|
|
|
@ -51,11 +54,11 @@ interface UploadFileProps {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const uploadRef = ref<UploadInstance>(); |
|
|
|
|
|
|
|
|
|
const loading = ref(false); |
|
|
|
|
let uploadPrecent = ref(0); |
|
|
|
|
const open = ref(false); |
|
|
|
|
const path = ref(); |
|
|
|
|
let uploadHelper = ref(''); |
|
|
|
|
|
|
|
|
|
const em = defineEmits(['close']); |
|
|
|
|
const handleClose = () => { |
|
|
|
@ -72,51 +75,76 @@ const fileOnChange = (_uploadFile: UploadFile, uploadFiles: UploadFiles) => {
|
|
|
|
|
|
|
|
|
|
const handleExceed: UploadProps['onExceed'] = (files) => { |
|
|
|
|
uploadRef.value!.clearFiles(); |
|
|
|
|
const file = files[0] as UploadRawFile; |
|
|
|
|
uploadRef.value!.handleStart(file); |
|
|
|
|
for (let i = 0; i < files.length; i++) { |
|
|
|
|
const file = files[i] as UploadRawFile; |
|
|
|
|
uploadRef.value!.handleStart(file); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const hadleSuccess: UploadProps['onSuccess'] = (res, file) => { |
|
|
|
|
console.log(file.name); |
|
|
|
|
file.status = 'success'; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const submit = async () => { |
|
|
|
|
loading.value = true; |
|
|
|
|
const file = uploaderFiles.value[0]; |
|
|
|
|
|
|
|
|
|
const CHUNK_SIZE = 1024 * 1024; // 1MB |
|
|
|
|
const fileSize = file.size; |
|
|
|
|
const chunkCount = Math.ceil(fileSize / CHUNK_SIZE); |
|
|
|
|
let uploadedChunkCount = 0; |
|
|
|
|
|
|
|
|
|
for (let i = 0; i < chunkCount; i++) { |
|
|
|
|
const start = i * CHUNK_SIZE; |
|
|
|
|
const end = Math.min(start + CHUNK_SIZE, fileSize); |
|
|
|
|
const chunk = file.raw.slice(start, end); |
|
|
|
|
|
|
|
|
|
const formData = new FormData(); |
|
|
|
|
|
|
|
|
|
formData.append('filename', file.name); |
|
|
|
|
formData.append('path', path.value); |
|
|
|
|
formData.append('chunk', chunk); |
|
|
|
|
formData.append('chunkIndex', i.toString()); |
|
|
|
|
formData.append('chunkCount', chunkCount.toString()); |
|
|
|
|
let success = 0; |
|
|
|
|
const files = uploaderFiles.value.slice(); |
|
|
|
|
for (let i = 0; i < files.length; i++) { |
|
|
|
|
const file = files[i]; |
|
|
|
|
const CHUNK_SIZE = 1024 * 1024; // 1MB |
|
|
|
|
const fileSize = file.size; |
|
|
|
|
|
|
|
|
|
uploadHelper.value = i18n.global.t('file.fileUploadStart', [file.name]); |
|
|
|
|
if (fileSize == 0) { |
|
|
|
|
const formData = new FormData(); |
|
|
|
|
formData.append('file', file.raw); |
|
|
|
|
formData.append('path', path.value); |
|
|
|
|
await UploadFileData(formData, {}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
await ChunkUploadFileData(formData, { |
|
|
|
|
onUploadProgress: (progressEvent) => { |
|
|
|
|
const progress = Math.round( |
|
|
|
|
((uploadedChunkCount + progressEvent.loaded / progressEvent.total) * 100) / chunkCount, |
|
|
|
|
); |
|
|
|
|
uploadPrecent.value = progress; |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
uploadedChunkCount++; |
|
|
|
|
} catch (error) { |
|
|
|
|
loading.value = false; |
|
|
|
|
break; |
|
|
|
|
const chunkCount = Math.ceil(fileSize / CHUNK_SIZE); |
|
|
|
|
let uploadedChunkCount = 0; |
|
|
|
|
for (let c = 0; c < chunkCount; c++) { |
|
|
|
|
const start = c * CHUNK_SIZE; |
|
|
|
|
const end = Math.min(start + CHUNK_SIZE, fileSize); |
|
|
|
|
const chunk = file.raw.slice(start, end); |
|
|
|
|
const formData = new FormData(); |
|
|
|
|
|
|
|
|
|
formData.append('filename', file.name); |
|
|
|
|
formData.append('path', path.value); |
|
|
|
|
formData.append('chunk', chunk); |
|
|
|
|
formData.append('chunkIndex', c.toString()); |
|
|
|
|
formData.append('chunkCount', chunkCount.toString()); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
await ChunkUploadFileData(formData, { |
|
|
|
|
onUploadProgress: (progressEvent) => { |
|
|
|
|
const progress = Math.round( |
|
|
|
|
((uploadedChunkCount + progressEvent.loaded / progressEvent.total) * 100) / chunkCount, |
|
|
|
|
); |
|
|
|
|
uploadPrecent.value = progress; |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
uploadedChunkCount++; |
|
|
|
|
} catch (error) { |
|
|
|
|
uploaderFiles.value[i].status = 'fail'; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
if (uploadedChunkCount == chunkCount) { |
|
|
|
|
success++; |
|
|
|
|
uploaderFiles.value[i].status = 'success'; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (uploadedChunkCount == chunkCount) { |
|
|
|
|
if (i == files.length - 1) { |
|
|
|
|
loading.value = false; |
|
|
|
|
uploadRef.value!.clearFiles(); |
|
|
|
|
uploaderFiles.value = []; |
|
|
|
|
MsgSuccess(i18n.global.t('file.uploadSuccess')); |
|
|
|
|
uploadHelper.value = ''; |
|
|
|
|
if (success == files.length) { |
|
|
|
|
uploadRef.value!.clearFiles(); |
|
|
|
|
uploaderFiles.value = []; |
|
|
|
|
MsgSuccess(i18n.global.t('file.uploadSuccess')); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
@ -125,6 +153,7 @@ const acceptParams = (props: UploadFileProps) => {
|
|
|
|
|
path.value = props.path; |
|
|
|
|
open.value = true; |
|
|
|
|
uploadPrecent.value = 0; |
|
|
|
|
uploadHelper.value = ''; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
defineExpose({ acceptParams }); |
|
|
|
|