Browse Source

fix(bugs): 图片上传

tangning 3 years ago
parent
commit
8e310cfaf9
1 changed files with 23 additions and 2 deletions
  1. 23 2
      src/components/Upload/src/UploadModal.vue

+ 23 - 2
src/components/Upload/src/UploadModal.vue

@@ -179,13 +179,33 @@
       //     imageList: [thumbUrl],
       //   });
       // }
-
+      /**
+       * 将中文符号转换成英文符号
+       */
+      function chineseChar2englishChar(chineseChar) {
+        // 将单引号‘’都转换成',将双引号“”都转换成"
+        var str = chineseChar.replace(/\’|\‘/g, "'").replace(/\“|\”/g, '"');
+        // 将中括号【】转换成[],将大括号{}转换成{}
+        str = str
+          .replace(/\【/g, '[')
+          .replace(/\】/g, ']')
+          .replace(/\{/g, '{')
+          .replace(/\}/g, '}');
+        // 将逗号,转换成,,将:转换成:
+        str = str.replace(/,/g, ',').replace(/:/g, ':');
+        return str;
+      }
       async function uploadApiByItem(item: FileItem) {
+        console.log('item', props, item);
         const { api, afterFetch } = props;
         if (!api || !isFunction(api)) {
           return warn('upload api must exist and be a function');
         }
         try {
+          let isZh = false;
+          if (/.*[\u4e00-\u9fa5]+.*$/.test(item.name)) {
+            isZh = true;
+          }
           item.status = UploadResultStatus.UPLOADING;
           const { data } = await props.api?.(
             {
@@ -194,7 +214,7 @@
               },
               file: item.file,
               name: props.name,
-              filename: props.filename,
+              filename: isZh ? item.uuid + '.' + item.type : props.filename,
             },
             function onUploadProgress(progressEvent: ProgressEvent) {
               const complete = ((progressEvent.loaded / progressEvent.total) * 100) | 0;
@@ -306,6 +326,7 @@
         handleCloseFunc,
         getIsSelectFile,
         getUploadBtnText,
+        chineseChar2englishChar,
         t,
       };
     },