function download(url, title) {
let a = document.createElement('a')
a.href = url
a.download = title
a.click()
}
如果想使用參數 title
重命名下載,必須遵循同源策略,如果跨域了,就按 url
末尾的文件名命名。
比如本地localhost運行代碼:
<a download="test.mp4" href="http://xxx.cn/abc.mp4">test</a>
下載下來就 abc.mp4
。
此外,title
需要自行添加文件後綴,如 .jpg
。