style: 上传增加进度条

pull/25/head
zhengkunwang223 2022-09-13 11:10:02 +08:00 committed by zhengkunwang223
parent 8a0aa168d2
commit 8e55e8e6ef
5 changed files with 41 additions and 14 deletions

View File

@ -11,7 +11,6 @@ const globalStore = GlobalStore();
const config = { const config = {
baseURL: import.meta.env.VITE_API_URL as string, baseURL: import.meta.env.VITE_API_URL as string,
timeout: ResultEnum.TIMEOUT as number, timeout: ResultEnum.TIMEOUT as number,
// 跨域时候允许携带凭证
withCredentials: true, withCredentials: true,
}; };
@ -81,6 +80,9 @@ class RequestHttp {
download<BlobPart>(url: string, params?: object, _object = {}): Promise<BlobPart> { download<BlobPart>(url: string, params?: object, _object = {}): Promise<BlobPart> {
return this.service.post(url, params, _object); return this.service.post(url, params, _object);
} }
upload<T>(url: string, params: object = {}, config: AxiosRequestConfig): Promise<T> {
return this.service.post(url, params, config);
}
} }
export default new RequestHttp(config); export default new RequestHttp(config);

View File

@ -1,5 +1,6 @@
import { File } from '@/api/interface/file'; import { File } from '@/api/interface/file';
import http from '@/api'; import http from '@/api';
import { AxiosRequestConfig } from 'axios';
export const GetFilesList = (params: File.ReqFile) => { export const GetFilesList = (params: File.ReqFile) => {
return http.post<File.File>('files/search', params); return http.post<File.File>('files/search', params);
@ -37,8 +38,8 @@ export const SaveFileContent = (params: File.FileEdit) => {
return http.post<File.File>('files/save', params); return http.post<File.File>('files/save', params);
}; };
export const UploadFileData = (params: FormData) => { export const UploadFileData = (params: FormData, config: AxiosRequestConfig) => {
return http.post<File.File>('files/upload', params); return http.upload<File.File>('files/upload', params, config);
}; };
export const RenameRile = (params: File.FileRename) => { export const RenameRile = (params: File.FileRename) => {

View File

@ -244,5 +244,6 @@ export default {
copy: 'Cpoy', copy: 'Cpoy',
calculate: 'Calculate', calculate: 'Calculate',
canNotDeCompress: 'Can not DeCompress this File', canNotDeCompress: 'Can not DeCompress this File',
uploadSuccess: 'Upload Success!',
}, },
}; };

View File

@ -244,5 +244,6 @@ export default {
copy: '', copy: '',
calculate: '', calculate: '',
canNotDeCompress: '', canNotDeCompress: '',
uploadSuccess: '!',
}, },
}; };

View File

@ -1,14 +1,24 @@
<template> <template>
<el-dialog v-model="open" :title="$t('file.upload')" @open="onOpen" :before-close="handleClose"> <el-dialog v-model="open" :title="$t('file.upload')" :before-close="handleClose" width="30%" :file-list="files">
<el-upload action="#" :auto-upload="false" ref="uploadRef" :multiple="true" :on-change="fileOnChange"> <el-upload
action="#"
:auto-upload="false"
ref="uploadRef"
:multiple="true"
:on-change="fileOnChange"
v-loading="loading"
>
<template #trigger> <template #trigger>
<el-button type="primary">{{ $t('file.selectFile') }}</el-button> <el-button type="primary">{{ $t('file.selectFile') }}</el-button>
</template> </template>
</el-upload> </el-upload>
<el-progress v-if="loading" :text-inside="true" :stroke-width="26" :percentage="uploadPrecent"></el-progress>
<template #footer> <template #footer>
<span class="dialog-footer"> <span class="dialog-footer">
<el-button @click="handleClose">{{ $t('commons.button.cancel') }}</el-button> <el-button @click="handleClose" :disabled="loading">{{ $t('commons.button.cancel') }}</el-button>
<el-button type="primary" @click="submit()">{{ $t('commons.button.confirm') }}</el-button> <el-button type="primary" @click="submit()" :disabled="loading">
{{ $t('commons.button.confirm') }}
</el-button>
</span> </span>
</template> </template>
</el-dialog> </el-dialog>
@ -18,6 +28,7 @@
import { ref } from 'vue'; import { ref } from 'vue';
import { ElMessage, UploadFile, UploadFiles, UploadInstance } from 'element-plus'; import { ElMessage, UploadFile, UploadFiles, UploadInstance } from 'element-plus';
import { UploadFileData } from '@/api/modules/files'; import { UploadFileData } from '@/api/modules/files';
import i18n from '@/lang';
const props = defineProps({ const props = defineProps({
open: { open: {
@ -31,9 +42,13 @@ const props = defineProps({
}); });
const uploadRef = ref<UploadInstance>(); const uploadRef = ref<UploadInstance>();
const files = ref();
const loading = ref(false);
let uploadPrecent = ref(0);
const em = defineEmits(['close']); const em = defineEmits(['close']);
const handleClose = () => { const handleClose = () => {
uploadRef.value!.clearFiles();
em('close', false); em('close', false);
}; };
@ -43,6 +58,11 @@ const fileOnChange = (_uploadFile: UploadFile, uploadFiles: UploadFiles) => {
uploaderFiles.value = uploadFiles; uploaderFiles.value = uploadFiles;
}; };
const onProcess = (e: any) => {
const { loaded, total } = e;
uploadPrecent.value = ((loaded / total) * 100) | 0;
};
const submit = () => { const submit = () => {
const formData = new FormData(); const formData = new FormData();
for (const file of uploaderFiles.value) { for (const file of uploaderFiles.value) {
@ -51,12 +71,14 @@ const submit = () => {
} }
} }
formData.append('path', props.path); formData.append('path', props.path);
loading.value = true;
UploadFileData(formData).then(() => { UploadFileData(formData, { onUploadProgress: onProcess })
ElMessage('upload success'); .then(() => {
handleClose(); ElMessage.success(i18n.global.t('file.uploadSuccess'));
}); handleClose();
})
.finally(() => {
loading.value = false;
});
}; };
const onOpen = () => {};
</script> </script>