1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>download</title> </head> <body> <button>Download</button> </body> <script> document.querySelector("button").addEventListener("click", () => { fetch("http://localhost:8889/api/download/file") .then(response => { const blob = response.blob(); const filename = response.headers.get("Content-Disposition"); console.log(filename); return { blob, filename }; }) .then(({ blob, filename }) => { blob.then(data => { // chrome const url = window.URL.createObjectURL(data); let link = document.createElement("a"); link.setAttribute("href", url); // link.setAttribute("download", filename.split("=")[1]); link.setAttribute("download", filename); link.click(); }); }); }); </script> </html>
|