Browse Source

修改下载

wangfumin 4 months ago
parent
commit
f6756522f3
2 changed files with 59 additions and 41 deletions
  1. 0 22
      src/store/mediaLibrary.ts
  2. 59 19
      src/view/mediaLibrary/index.vue

+ 0 - 22
src/store/mediaLibrary.ts

@@ -69,33 +69,11 @@ export const deleteMediaItem = async (id) =>
 // 下载媒体
 // 下载媒体
 export const downloadMedia = async (id) => {
 export const downloadMedia = async (id) => {
   const response = await axios.get(downFile, { params: {dictFileId: id} });
   const response = await axios.get(downFile, { params: {dictFileId: id} });
-  if (response.data) {
-    // 创建一个隐藏的a标签
-    const link = document.createElement('a');
-    link.href = response.data;
-    // 从URL中提取文件名
-    const fileName = response.data.split('/').pop() || `file_${id}`;
-    link.setAttribute('download', fileName);
-    document.body.appendChild(link);
-    link.click();
-    document.body.removeChild(link);
-  }
   return response.data;
   return response.data;
 }
 }
 
 
 // 下载hash
 // 下载hash
 export const downloadHash = async (id) => {
 export const downloadHash = async (id) => {
   const response = await axios.get(downhash, { params: {dictFileId: id} });
   const response = await axios.get(downhash, { params: {dictFileId: id} });
-  if (response.data) {
-    // 创建一个隐藏的a标签
-    const link = document.createElement('a');
-    link.href = response.data;
-    // 从URL中提取文件名
-    const fileName = response.data.split('/').pop() || `hash_${id}.txt`;
-    link.setAttribute('download', fileName);
-    document.body.appendChild(link);
-    link.click();
-    document.body.removeChild(link);
-  }
   return response.data;
   return response.data;
 }
 }

+ 59 - 19
src/view/mediaLibrary/index.vue

@@ -231,16 +231,50 @@ const handleDownload = async (row: Media, type: 'media' | 'hash') => {
     if (type === 'media') {
     if (type === 'media') {
       if (row.downUrl) {
       if (row.downUrl) {
         url = row.downUrl;
         url = row.downUrl;
+        fileName = `${row.fileName}.${row.fileFormat}` || `file_${row.id} `
       } else {
       } else {
         // 通过接口获取URL
         // 通过接口获取URL
         url = await downloadMedia(row.id);
         url = await downloadMedia(row.id);
+        fileName = url.split('/').pop() || `file_${row.id}`;
         if (!url) {
         if (!url) {
           ElMessage.error('获取下载链接失败');
           ElMessage.error('获取下载链接失败');
           return;
           return;
         }
         }
       }
       }
-      fileName = `${row.fileName}.${row.fileFormat}` || url.split('/').pop() || `file_${row.id}`;
+      
+      // 判断文件类型
+      const isMediaFile = ['0', '1', '2', 0, 1, 2].includes(row.fileType); // 0:图片, 1:视频, 2:音频
+      ElMessage.success('正在下载文件...')
+      if (isMediaFile) {
+        // 对于媒体文件(图片、视频、音频)使用blob下载
+        try {
+          // 使用fetch获取文件内容
+          const response = await fetch(url);
+          const blob = await response.blob();
+          
+          // 创建Blob URL
+          const blobUrl = window.URL.createObjectURL(blob);
+          
+          // 创建下载链接
+          const link = document.createElement('a');
+          link.href = blobUrl;
+          link.setAttribute('download', fileName);
+          document.body.appendChild(link);
+          link.click();
+          
+          // 清理
+          document.body.removeChild(link);
+          window.URL.revokeObjectURL(blobUrl);
+        } catch (error) {
+          console.error('媒体文件下载失败:', error);
+          ElMessage.error('媒体文件下载失败');
+        }
+      } else {
+        // 对于其他类型文件,直接打开链接下载
+        window.open(url);
+      }
     } else {
     } else {
+      // Hash文件下载
       if (row.hashUrl) {
       if (row.hashUrl) {
         url = row.hashUrl;
         url = row.hashUrl;
       } else {
       } else {
@@ -252,26 +286,32 @@ const handleDownload = async (row: Media, type: 'media' | 'hash') => {
         }
         }
       }
       }
       fileName = `${row.fileName || 'file'}.txt`;
       fileName = `${row.fileName || 'file'}.txt`;
+      
+      // Hash文件通常是文本文件,使用blob下载
+      try {
+        // 使用fetch获取文件内容
+        const response = await fetch(url);
+        const blob = await response.blob();
+        
+        // 创建Blob URL
+        const blobUrl = window.URL.createObjectURL(blob);
+        
+        // 创建下载链接
+        const link = document.createElement('a');
+        link.href = blobUrl;
+        link.setAttribute('download', fileName);
+        document.body.appendChild(link);
+        link.click();
+        
+        // 清理
+        document.body.removeChild(link);
+        window.URL.revokeObjectURL(blobUrl);
+      } catch (error) {
+        console.error('Hash文件下载失败:', error);
+        ElMessage.error('Hash文件下载失败');
+      }
     }
     }
     
     
-    // 使用fetch获取文件内容
-    const response = await fetch(url);
-    const blob = await response.blob();
-    
-    // 创建Blob URL
-    const blobUrl = window.URL.createObjectURL(blob);
-    
-    // 创建下载链接
-    const link = document.createElement('a');
-    link.href = blobUrl;
-    link.setAttribute('download', fileName);
-    document.body.appendChild(link);
-    link.click();
-    
-    // 清理
-    document.body.removeChild(link);
-    window.URL.revokeObjectURL(blobUrl);
-    
     ElMessage.success(type === 'media' ? '下载成功' : 'Hash下载成功');
     ElMessage.success(type === 'media' ? '下载成功' : 'Hash下载成功');
   } catch (error) {
   } catch (error) {
     console.error(type === 'media' ? '下载失败:' : 'Hash下载失败:', error);
     console.error(type === 'media' ? '下载失败:' : 'Hash下载失败:', error);