Js唤醒下载文件




  1. function openDownload(url){
  2. let d = new Date().getTime();
  3. let saveName=d+"."+ url.replace(/(.*\.)/, '');//这里文件名我用了毫秒数加上后缀
  4. var $a = document.createElement('a');
  5. $a.setAttribute("href", url);
  6. $a.setAttribute("download", saveName);
  7. $a.setAttribute("target","_blank");//弹出窗体
  8. //模拟js事件
  9. var evObj = document.createEvent('MouseEvents');
  10. evObj.initMouseEvent( 'click', true, true, window, 0, 0, 0, 0, 0, false, false, true, false, 0, null);
  11. $a.dispatchEvent(evObj);
  12. }

参考

https://blog.csdn.net/weixin_42368893/article/details/99644970