H5页面实现下载文件(apk、txt等)的三种方式

方法1:
window.location.href = 'http://imtt.dd.qq.com/16891/26747DD8B125A8479AD0C9D22CA47BC9.apk?fsname=com.snda.wifilocating_4.2.91_3211.apk&csr=1bbd';
空白页是因为我改变href为apk的下载链接,网页打开的时候会停留在一个空白页,然后一般手机的状态栏上会出现下载apk的进度条。
既然不想出现空白页面的话,那不直接打开一个页面而改成在当前页打开就可以了。 
方法2:

    1. var src = 'http://imtt.dd.qq.com/16891/26747DD8B125A8479AD0C9D22CA47BC9.apk?fsname=com.snda.wifilocating_4.2.91_3211.apk&csr=1bbd';
    2.  
      var iframe = document.createElement('iframe');
    3.  
      iframe.style.display = 'none';
    4.  
      iframe.src = "javascript: '<script>location.href="" + src + ""</script>'";
    5.  
      document.getElementsByTagName('body')[0].appendChild(iframe);
方法3:
var src = 'http://imtt.dd.qq.com/16891/26747DD8B125A8479AD0C9D22CA47BC9.apk?fsname=com.snda.wifilocating_4.2.91_3211.apk&csr=1bbd';
var form = document.createElement('form');
form.action = src;
document.getElementsByTagName('body')[0].appendChild(form);
form.submit();

转载:https://blog.csdn.net/weixin_34221112/article/details/88768839

原文地址:https://www.cnblogs.com/huanhuan55/p/11457199.html