Просмотр исходного кода

修改下载文件名,上传限制文件大小

wangfumin 3 месяцев назад
Родитель
Сommit
4a2fa7a7a0
2 измененных файлов с 75 добавлено и 9 удалено
  1. 14 9
      src/view/mediaLibrary/index.vue
  2. 61 0
      src/view/mediaLibrary/uploadMedia.vue

+ 14 - 9
src/view/mediaLibrary/index.vue

@@ -238,21 +238,26 @@ const handleDownload = async (row: Media, type: 'media' | 'hash') => {
     if (type === 'media') {
       if (row.downUrl) {
         url = row.downUrl;
-        fileName = `${row.fileName}.${row.fileFormat}` || `file_${row.id} `
+        // 从URL中获取文件后缀
+        const urlParts = url.split('.');
+        const fileExt = urlParts.length > 1 ? urlParts[urlParts.length - 1].split('?')[0] : row.fileFormat;
+        fileName = `${row.fileName}.${fileExt}` || `file_${row.id}.${fileExt}`;
       } else {
         // 通过接口获取URL
         url = await downloadMedia(row.id);
-        fileName = url.split('/').pop() || `file_${row.id}`;
         if (!url) {
           ElMessage.error('获取下载链接失败');
           return;
         }
+        // 从URL中获取文件后缀
+        const urlParts = url.split('.');
+        const fileExt = urlParts.length > 1 ? urlParts[urlParts.length - 1].split('?')[0] : row.fileFormat;
+        fileName = `${row.fileName}.${fileExt}` || `file_${row.id}.${fileExt}`;
       }
-      
       // 判断文件类型
       const isMediaFile = ['0', '1', '2', 0, 1, 2].includes(row.fileType); // 0:图片, 1:视频, 2:音频
-      ElMessage.success('正在下载文件...')
-      if (isMediaFile) {
+
+      // if (isMediaFile) {
         // 对于媒体文件(图片、视频、音频)使用blob下载
         try {
           // 使用fetch获取文件内容
@@ -276,10 +281,10 @@ const handleDownload = async (row: Media, type: 'media' | 'hash') => {
           console.error('媒体文件下载失败:', error);
           ElMessage.error('媒体文件下载失败');
         }
-      } else {
-        // 对于其他类型文件,直接打开链接下载
-        window.open(url);
-      }
+      // } else {
+      //   // 对于其他类型文件,直接打开链接下载
+      //   window.open(url);
+      // }
     } else {
       // Hash文件下载
       if (row.hashUrl) {

+ 61 - 0
src/view/mediaLibrary/uploadMedia.vue

@@ -126,9 +126,70 @@ const fileList = ref([]);
 
 // 处理文件变化
 const handleFileChange = (file, files) => {
+  // 检查文件类型和大小
+  const isValidFile = validateFile(file);
+  if (!isValidFile) {
+    // 如果文件无效,从文件列表中移除
+    const index = files.indexOf(file);
+    if (index !== -1) {
+      files.splice(index, 1);
+    }
+    return;
+  }
+  
   fileList.value = files;
 };
 
+// 验证文件类型和大小
+const validateFile = (file) => {
+  if (!file || !file.raw) return false;
+  
+  const fileName = file.name || '';
+  const fileSize = file.size || 0;
+  const fileExt = fileName.split('.').pop().toLowerCase();
+  
+  // 检查文件类型
+  if (selectedGroup.value == 1) {
+    // 模型库组只允许 obj、glb、zip 格式
+    if (!['obj', 'glb', 'zip'].includes(fileExt)) {
+      ElMessage.error(`不支持的文件类型`);
+      return false;
+    }
+    
+    // 检查文件大小,不能超过 5MB
+    const maxSize = 5 * 1024 * 1024; // 5MB
+    if (fileSize > maxSize) {
+      ElMessage.error(`上传文件不能超过 5MB`);
+      return false;
+    }
+  } else {
+    // 其他组允许 jpg、png、jpeg、mp4、wav、mp3、shp、zip 格式
+    if (!['jpg', 'png', 'jpeg', 'mp4', 'wav', 'mp3', 'shp', 'zip'].includes(fileExt)) {
+      ElMessage.error(`不支持的文件格式`);
+      return false;
+    }
+    
+    // 检查文件大小
+    if (fileExt === 'zip') {
+      // zip 文件不能超过 1GB
+      const maxSize = 1 * 1024 * 1024 * 1024; // 1GB
+      if (fileSize > maxSize) {
+        ElMessage.error(`上传文件不能超过 1GB`);
+        return false;
+      }
+    } else {
+      // 其他文件不能超过 2GB
+      const maxSize = 2 * 1024 * 1024 * 1024; // 2GB
+      if (fileSize > maxSize) {
+        ElMessage.error(`上传文件不能超过 2GB`);
+        return false;
+      }
+    }
+  }
+  
+  return true;
+};
+
 // 处理文件删除
 const handleFileRemove = (file, fileList) => {
   handleFileChange({}, []);