feat: 修改上传统一参数格式&上传组件提取参数

This commit is contained in:
xinxin.wu 2023-07-20 16:47:54 +08:00 committed by 刘瑞斌
parent 468a8d8865
commit 45f923d752
3 changed files with 34 additions and 21 deletions

View File

@ -83,28 +83,16 @@ export class MSAxios {
*/ */
uploadFile<T = any>(config: AxiosRequestConfig, params: UploadFileParams) { uploadFile<T = any>(config: AxiosRequestConfig, params: UploadFileParams) {
const formData = new window.FormData(); const formData = new window.FormData();
const customFilename = params.name || 'file'; const fileName = params.fileList.length === 1 ? 'file' : 'files';
if (params.filename) { params.fileList.forEach((file: File) => {
formData.append(customFilename, params.file, params.filename); formData.append(fileName, file);
} else { });
formData.append(customFilename, params.file); if (params.request) {
const requestData = JSON.stringify(params.request);
const requestDataBlob = new Blob([requestData], { type: 'application/json' });
formData.append('request', requestDataBlob);
} }
if (params.data) {
Object.keys(params.data).forEach((key) => {
const value = params.data[key];
if (Array.isArray(value)) {
value.forEach((item) => {
formData.append(`${key}[]`, item);
});
return;
}
formData.append(key, params.data[key]);
});
}
return this.axiosInstance.request<T>({ return this.axiosInstance.request<T>({
...config, ...config,
method: 'POST', method: 'POST',

View File

@ -3,7 +3,7 @@
<template #upload-button> <template #upload-button>
<div class="ms-upload-area"> <div class="ms-upload-area">
<div class="ms-upload-icon-box"> <div class="ms-upload-icon-box">
<div class="ms-upload-icon ms-upload-icon--excel"></div> <div class="ms-upload-icon" :class="`ms-upload-icon--${props.iconType}`"></div>
</div> </div>
<div v-if="props.mainText" class="ms-upload-main-text">{{ t(props.mainText) }}</div> <div v-if="props.mainText" class="ms-upload-main-text">{{ t(props.mainText) }}</div>
<div v-if="props.subText" class="ms-upload-sub-text">{{ t(props.subText) }}</div> <div v-if="props.subText" class="ms-upload-sub-text">{{ t(props.subText) }}</div>
@ -29,6 +29,7 @@
limit: number; limit: number;
imagePreview: boolean; imagePreview: boolean;
showFileList: boolean; showFileList: boolean;
iconType: string;
[key: string]: any; [key: string]: any;
}> & { }> & {
accept: UploadType; accept: UploadType;
@ -86,6 +87,9 @@
&--image { &--image {
background-image: url('@/assets/svg/icons/image.svg'); background-image: url('@/assets/svg/icons/image.svg');
} }
&--jar {
background-image: url('@/assets/svg/icons/jar.svg');
}
} }
} }
.ms-upload-main-text { .ms-upload-main-text {

View File

@ -99,3 +99,24 @@ export function sleep(ms: number): Promise<void> {
setTimeout(() => resolve(), ms); setTimeout(() => resolve(), ms);
}); });
} }
/**
*
*
* @param fileSize file文件的大小size
* @returns
*/
export function formatFileSize(fileSize: number): string {
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
let size = fileSize;
let unitIndex = 0;
while (size >= 1024 && unitIndex < units.length - 1) {
size /= 1024;
unitIndex++;
}
const formattedSize = size.toFixed(2);
const unit = units[unitIndex];
return `${formattedSize} ${unit}`;
}