使用jQuery下载文件 [英] Download File Using jQuery

查看:118
本文介绍了使用jQuery下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户点击一个链接,我如何提示下载。

How can I prompt a download for a user when they click a link.

例如,而不是:

<a href="uploads/file.doc">Download Here</a>

我可以使用:

<a href="#">Download Here</a>

 $('a').click... //Some jquery to download the file

这样一来,Google不会对我的HREF和私人文件进行索引。

This way, Google does not index my HREF's and private files.

可以使用jQuery完成这个操作吗?或者应该使用PHP或其他东西?

Can this be done with jQuery, if so, how? Or should this be done with PHP or something instead?

推荐答案

我可能会建议,作为一个更优雅的降级解决方案,使用< a href =http://docs.jquery.com/Events/jQuery.Event#event.preventDefault.28.29 =noreferrer> preventDefault :$ {

$ b

I might suggest this, as a more gracefully degrading solution, using preventDefault:

$('a').click(function(e) {
    e.preventDefault();  //stop the browser from following
    window.location.href = 'uploads/file.doc';
});

<a href="no-script.html">Download now!</a>

即使没有Javascript,至少这样一来,用户将获得一些反馈。

Even if there's no Javascript, at least this way the user will get some feedback.

这篇关于使用jQuery下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆