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

查看:57
本文介绍了使用 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?

推荐答案

我可能会建议使用 preventDefault:

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天全站免登陆