Bladeren bron

资料导入下载修改

duy 1 maand geleden
bovenliggende
commit
17b7cf3fdd
1 gewijzigde bestanden met toevoegingen van 50 en 1 verwijderingen
  1. 50 1
      src/views/data-fill/components/dataImport.vue

+ 50 - 1
src/views/data-fill/components/dataImport.vue

@@ -172,6 +172,55 @@ const confirmTap = async ()=>{
 
 
     
+}
+const parseFileName = (disposition) => {
+    const filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/
+    const matches = filenameRegex.exec(disposition)
+    if (matches && matches[1]) {
+        return decodeURIComponent(matches[1].replace(/['"]/g, ''))
+    }
+    return 'unknown'
+}
+
+
+const downloadBlob1 = (data, disposition = '', type = 'application/vnd.ms-excel') =>{
+    const blob = new Blob([data], { type: type })
+    const blobURL = window.URL.createObjectURL(blob)
+    const tempLink = document.createElement('a')
+    tempLink.style.display = 'none'
+    tempLink.href = blobURL
+    
+    // 1. 解析文件名
+    let filename = parseFileName(disposition)
+    
+    // 2. 关键修复:处理文件名(解码 + 过滤特殊字符 + 确保后缀)
+    try {
+        // 先解码URL编码的字符(如%E6转为中文)
+        filename = decodeURIComponent(filename)
+    } catch (e) {
+        // 若解码失败(如特殊字符不完整),直接使用原始值
+        console.warn('文件名解码失败,使用原始值', e)
+    }
+    
+    // 过滤不安全字符:保留中文、英文、数字、-、+、_、.,其他替换为_
+    filename = filename.replace(/[#%/\\:*?"<>|]/g, '_')
+    
+    // 确保文件名以.xlsx结尾(若被截断或误加字符,强制修正)
+    if (!filename.endsWith('.xlsx')) {
+        // 移除现有后缀(如果有),再补全.xlsx
+        filename = filename.replace(/\.[^.]*$/, '') + '.xlsx'
+    }
+    
+    // 3. 设置处理后的文件名
+    tempLink.setAttribute('download', filename)
+    
+    if (typeof tempLink.download === 'undefined') {
+        tempLink.setAttribute('target', '_blank')
+    }
+    document.body.appendChild(tempLink)
+    tempLink.click()
+    document.body.removeChild(tempLink)
+    window.URL.revokeObjectURL(blobURL)
 }
 //导入模板
 const downLoadTemplate = async ()=>{
@@ -184,7 +233,7 @@ const downLoadTemplate = async ()=>{
     downLoadTemplateLoading.value = false
     if (!error) {
         if (disposition) {
-            downloadBlob(res, disposition)
+            downloadBlob1(res, disposition)
         } else {
             window.$message?.error(msg || '数据异常')
         }