Browse Source

产品导入修改

moshaorui 12 hours ago
parent
commit
baa4740ba7
1 changed files with 49 additions and 16 deletions
  1. 49 16
      app/Libraries/CommonHelper.php

+ 49 - 16
app/Libraries/CommonHelper.php

@@ -209,24 +209,57 @@ class CommonHelper
             $('#videoPopup iframe').attr('src', videoUrl); // 设置 iframe 的 src
         });
 
-        $('.downloadVideo').on('click', function(e) {
-            e.preventDefault();
-            const videoUrl = $(this).attr('videoUrl');
-            const \$tempLink = $('<a>', {
-                href: videoUrl,
-                download: ''
-            }).hide();
-            $('body').append(\$tempLink);
-            \$tempLink[0].click();
-            \$tempLink.remove();
-            setTimeout(() => {
-                if(!document.querySelector('a[download]')) {
-                    console.error('下载失败:链接无效或CORS限制');
-                    alert('视频下载失败,请联系管理员');
+$('.downloadVideo').on('click', function(e) {
+    e.preventDefault();
+    const videoUrl = $(this).attr('videoUrl');
+
+    const progressBar = $('<div class=\"progress-bar\">').appendTo('body');
+
+    fetch(videoUrl, {
+        headers: new Headers({'Origin': location.origin}),
+        mode: 'cors'
+    })
+    .then(response => {
+        if (!response.ok) throw new Error('HTTP状态码异常');
+        const reader = response.body.getReader();
+        const contentLength = +response.headers.get('Content-Length');
+        let receivedLength = 0;
+        const chunks = [];
+
+        // 流式处理大文件
+        function processChunk({ done, value }) {
+            if (done) {
+                const blob = new Blob(chunks);
+                const filename = videoUrl.split('/').pop() || 'video.mp4';
+                const objectUrl = URL.createObjectURL(blob);
+
+                // 使用FileSaver增强兼容性(网页8方案)
+                if (window.saveAs) {
+                    saveAs(objectUrl, filename);
+                } else {
+                    const tempLink = document.createElement('a');
+                    tempLink.href = objectUrl;
+                    tempLink.download = filename;
+                    tempLink.click();
                 }
-            }, 1000);
 
-        });
+                progressBar.remove();
+                return;
+            }
+
+            chunks.push(value);
+            receivedLength += value.length;
+            progressBar.css('width', `\${(receivedLength/contentLength)*100}%`);
+            return reader.read().then(processChunk);
+        }
+
+        return reader.read().then(processChunk);
+    })
+    .catch(error => {
+        console.error('下载失败:', error);
+        alert('视频下载失败,请检查网络或文件有效性');
+    });
+});
 
         // 点击关闭按钮关闭视频
         $('.close-btn').on('click', function() {