简体   繁体   English

使用javascript / jquery下载文件

[英]File download with javascript/jquery

I'm trying to download an apk file from my webapp using a tablet. 我正在尝试使用平板电脑从我的Web应用程序下载apk文件。

<a href="#" data-role="button" id="loginButton" class="pulsantino">Login</a>

$("#pulsanteLogin").live("click",function(){
     alert("1");
     window.location.href = 'http://xx.yy.zz.www:1234/staticResources/Myapp_version.apk';
     alert("2");
}

I know for sure that the url is correct (launching it from the browser will correctly start file download), but when i click on the button i get no error and no file download. 我肯定知道URL是正确的(从浏览器启动它会正确开始文件下载),但是当我单击该按钮时,我没有错误,也没有文件下载。 I get both the alerts for '1' and '2'. 我同时收到“ 1”和“ 2”的警报。

I've tried also with 我也尝试过

location.href = 'url';

and

window.location = 'url';

Ideas? 想法?

Maybe this is too obvious, but why dont you just use a regular link like this directly: 也许这太明显了,但是为什么不直接使用这样的常规链接:

<a href="http://xx.yy.zz.www:1234/staticResources/Myapp_version.apk" data-role="button" id="loginButton" class="pulsantino">Login</a>

Are you executing more code which requires the dynamic change of this link's href attribute? 您是否正在执行更多需要动态更改此链接的href属性的代码?

Are you using a newer version of jQuery than 1.7? 您是否正在使用比1.7更高的jQuery版本? .live() got deprecated, and .on() is now the way to go. .live()已被弃用,.on()现在已成为主流。 http://api.jquery.com/on/ http://api.jquery.com/on/

As of jQuery 1.7, the .live() method is deprecated. 从jQuery 1.7开始,不推荐使用.live()方法。 Use .on() to attach event handlers. 使用.on()附加事件处理程序。 Users of older versions of jQuery should use .delegate() in preference to .live(). 较旧版本的jQuery的用户应优先使用.delegate()而不是.live()。 Source: http://api.jquery.com/live/ 资料来源: http//api.jquery.com/live/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM