关于使用a标签下载图片视频的问题解决

      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

发布了59 篇原创文章 · 获赞 78 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/PrisonersDilemma/article/details/104889177