JS文件下载 发表于 2024-03-05 | 分类于 js JS文件下载 根据URL下载文件1234567891011121314151617181920212223<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"></head><body> <button onclick="downloadFile('https://example.com/sample.pdf')">下载文件</button> <script> function downloadFile(url) { const a = document.createElement('a'); a.href = url; a.target = '_blank'; a.download = url.split('/').pop(); document.body.appendChild(a); a.click(); document.body.removeChild(a); } </script></body></html> JS 123456789export function downloadFile(url) { const a = document.createElement('a') a.href = url a.target = '_blank' a.download = url.split('/').pop() document.body.appendChild(a) a.click() document.body.removeChild(a)}