用js 触发 浏览器的下载功能下载文件

主要解决页面内部动态获取下载文件url后,自动下载的问题。


新开一个标签页下载: window.open('url', '_blank');

这会导致屏幕新开标签页时的闪动,但浏览器兼容性好。


html5浏览器中, 可在本标签页中无闪动开启下载:

$('<a href="path_to_file" download="proposed_file_name">Download</a>')[0].click();
  • path_to_file is either an absolute or relative path,
  • proposed_file_name the 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/11620698/how-to-trigger-a-file-download-when-clicking-an-html-button-or-javascript


http://stackoverflow.com/questions/5811122/how-to-trigger-a-click-on-a-link-using-jquery


猜你喜欢

转载自blog.csdn.net/jdk137/article/details/51672841
今日推荐