1 // 彿é®ç¹å»æ¶è·åé¢ç¾åURLå¹¶ä¸è½½ 2 function getSignedUrlAndDownload(fileName) { 3 // const privacyUrl = "http://api.miaomiaoce.com:4301/generate_presigned_url/?file_name=" + fileName; 4 const privacyUrl = "https://api.miaomiaoce.com:4301/generate_presigned_url/?file_name=" + encodeURIComponent(fileName) 5 fetch(privacyUrl) 6 .then(response => { 7 if (!response.ok) { 8 throw new Error('Network response was not ok ' + response.statusText); 9} 10 return response.json(); // å设è¿åçæ¯ JSON æ ¼å¼ 11}) 12 .then(data => { 13 // å设è¿åçæ°æ®ä¸æ `url` åæ®µ 14 const downloadUrl = data.url; 15 if (downloadUrl) { 16 // ç´æ¥ä¸è½½æä»¶ 17 // window.location.href = downloadUrl; 18 // downloadFile(downloadUrl, fileName); 19 window.open(downloadUrl, '_blank'); 20 21} else { 22 console.error('No URL found in the response:', data); 23} 24}) 25 .catch(error => { 26 console.error('Fetch error for file:', fileName, error); 27}); 28} 29 30 function downloadFile(url, filename) { 31 // 使ç¨fetch APIè·åæä»¶ 32 fetch(url) 33 .then(response => { 34 // ç¡®ä¿ååºæ¯æåç 35 if (!response.ok) { 36 throw new Error('Network response was not ok.'); 37 } 38 // å°ååºä½è½¬æ¢ä¸ºBlob 39 return response.blob(); 40 }) 41 .then(blob => { 42 // å建ä¸ä¸ªæåBlobçURL 43 const blobUrl = window.URL.createObjectURL(blob); 44 // å建ä¸ä¸ª<a>å ç´ å¹¶è®¾ç½®href为Blob URL 45 const link = document.createElement('a'); 46 link.href = blobUrl; 47 link.download = filename || 'downloaded.file'; // 设置ä¸è½½çæä»¶å 48 // å°<a>å ç´ éè 49 link.style.display = 'none'; 50 // å°<a>å ç´ æ·»å å°ææ¡£ä¸ 51 document.body.appendChild(link); 52 // 触åç¹å»äºä»¶ä»¥ä¸è½½æä»¶ 53 link.click(); 54 // æ¸ çï¼ç§»é¤<a>å ç´ å¹¶éæ¾Blob URL 55 document.body.removeChild(link); 56 window.URL.revokeObjectURL(blobUrl); 57 }) 58 .catch(error => { 59 console.error('There was a problem with the download:', error); 60 }); 61} 62 63 // ä½¿ç¨æ¹æ³ 64 // downloadFile('https://example.com/path/to/file', 'desired-filename.ext'); 65 66 67 // 为ææä¸è½½æé®æ·»å äºä»¶çå¬ 68 document.querySelectorAll('.btn-download').forEach(button => { 69 const fileName = button.getAttribute('data-filename'); 70 71 button.addEventListener('click', function(event) { 72 event.preventDefault(); // 鲿¢é»è®¤è¡ä¸º 73 getSignedUrlAndDownload(fileName); // è°ç¨å½æ°è·åé¢ç¾åURLå¹¶ä¸è½½ 74}); 75});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.