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

来源:互联网 发布:无线点菜系统源码 编辑:程序博客网 时间:2024/05/29 14:12

主要解决页面内部动态获取下载文件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).
参考文献:

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




0 0
原创粉丝点击