Prechádzať zdrojové kódy

feat(Upload): 兼容ant-design-vue的upload属性 (#1247)

1. 兼容`name`属性,用于自定义发到后台的文件参数名; 2. 兼容`filename`属性

Co-authored-by: erniu <joe.cheng237@gmail.com>
erniu 3 rokov pred
rodič
commit
935d4fc12d

+ 5 - 1
src/components/Upload/src/UploadModal.vue

@@ -187,8 +187,12 @@
           item.status = UploadResultStatus.UPLOADING;
           const { data } = await props.api?.(
             {
-              ...(props.uploadParams || {}),
+              data: {
+                ...(props.uploadParams || {}),
+              },
               file: item.file,
+              name: props.name,
+              filename: props.filename,
             },
             function onUploadProgress(progressEvent: ProgressEvent) {
               const complete = ((progressEvent.loaded / progressEvent.total) * 100) | 0;

+ 8 - 0
src/components/Upload/src/props.ts

@@ -34,6 +34,14 @@ export const basicProps = {
     default: null,
     required: true,
   },
+  name: {
+    type: String as PropType<string>,
+    default: 'file',
+  },
+  filename: {
+    type: String as PropType<string>,
+    default: null,
+  },
 };
 
 export const uploadContainerProps = {

+ 10 - 10
src/utils/http/axios/Axios.ts

@@ -5,7 +5,7 @@ import axios from 'axios';
 import qs from 'qs';
 import { AxiosCanceler } from './axiosCancel';
 import { isFunction } from '/@/utils/is';
-import { cloneDeep, omit } from 'lodash-es';
+import { cloneDeep } from 'lodash-es';
 import { ContentTypeEnum } from '/@/enums/httpEnum';
 import { RequestEnum } from '/@/enums/httpEnum';
 
@@ -121,11 +121,17 @@ export class VAxios {
    */
   uploadFile<T = any>(config: AxiosRequestConfig, params: UploadFileParams) {
     const formData = new window.FormData();
+    const customFilename = params.name || 'file';
+
+    if (params.filename) {
+      formData.append(customFilename, params.file, params.filename);
+    } else {
+      formData.append(customFilename, params.file);
+    }
 
     if (params.data) {
       Object.keys(params.data).forEach((key) => {
-        if (!params.data) return;
-        const value = params.data[key];
+        const value = params.data![key];
         if (Array.isArray(value)) {
           value.forEach((item) => {
             formData.append(`${key}[]`, item);
@@ -133,15 +139,9 @@ export class VAxios {
           return;
         }
 
-        formData.append(key, params.data[key]);
+        formData.append(key, params.data![key]);
       });
     }
-    formData.append(params.name || 'file', params.file, params.filename);
-    const customParams = omit(params, 'file', 'filename', 'file');
-
-    Object.keys(customParams).forEach((key) => {
-      formData.append(key, customParams[key]);
-    });
 
     return this.axiosInstance.request<T>({
       ...config,