大型文件请考虑使用 filesaver.js
https://github.com/eligrey/FileSaver.js/
==============================================
主要解决页面内部动态获取下载文件url后,自动下载的问题。
新开一个标签页下载: window.open('url', '_blank');
这会导致屏幕新开标签页时的闪动,但浏览器兼容性好。
html5浏览器中, 可在本标签页中无闪动开启下载:
$('<a href="path_to_file" download="proposed_file_name">Download</a>')[0].click();
path_to_fileis either an absolute or relative path,proposed_file_namethe filename to save to (can be blank, then defaults to the actual filename).
没有jquery时:
var a = document.createElement('a');
a.href = "path_to_file";
a.download = "proposed_file_name";
a.click();
参考文献:
http://stackoverflow.com/questions/5811122/how-to-trigger-a-click-on-a-link-using-jquery
本文介绍了如何在JavaScript中触发浏览器的下载功能,特别是在页面内动态获取下载URL时。推荐在处理大型文件时使用FileSaver.js库。针对新开标签页下载和HTML5浏览器中的无闪动下载提供了代码示例,并引用了StackOverflow上的相关讨论。

2130

被折叠的 条评论
为什么被折叠?



