guoziyun 1 rok temu
rodzic
commit
9dee42110c
1 zmienionych plików z 33 dodań i 7 usunięć
  1. 33 7
      views/detail.ejs

+ 33 - 7
views/detail.ejs

@@ -70,15 +70,41 @@
 
 
             <script>
+                // document.getElementById('downloadBtn').addEventListener('click', function () {
+                //     var link = document.createElement('a');
+                //     link.style.display = 'none';
+                //     link.href = '<%= detail.downlink %>';
+                //     link.download = '<%= detail._id %>.jpeg';
+                //     document.body.appendChild(link);
+                //     link.click();
+                //     document.body.removeChild(link);
+                // });
+
                 document.getElementById('downloadBtn').addEventListener('click', function () {
-                    var link = document.createElement('a');
-                    link.style.display = 'none';
-                    link.href = '<%= detail.downlink %>';
-                    link.download = '<%= detail._id %>.jpeg';
-                    document.body.appendChild(link);
-                    link.click();
-                    document.body.removeChild(link);
+                    const imageUrl = '<%= detail.downlink %>';
+                    fetch(imageUrl)
+                        .then(response => {
+                            if (!response.ok) {
+                                throw new Error('Network response was not ok');
+                            }
+                            return response.blob();
+                        })
+                        .then(blob => {
+                            const url = URL.createObjectURL(blob);
+                            const a = document.createElement('a');
+                            a.style.display = 'none';
+                            a.href = url;
+                            a.download = '<%= detail._id %>.jpeg';
+                            document.body.appendChild(a);
+                            a.click();
+                            URL.revokeObjectURL(url);
+                            document.body.removeChild(a);
+                        })
+                        .catch(error => {
+                            console.error('Image Download Error:', error);
+                        });
                 });
+
             </script>
 </body>