使用 jQuery 下载文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1296085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:08:25  来源:igfitidea点击:

Download File Using jQuery

jqueryfiledownload

提问by Dodinas

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

当用户单击链接时,如何提示用户下载。

For example, instead of:

例如,而不是:

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

I could use:

我可以使用:

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

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

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

这样,Google 就不会索引我的 HREF 和私人文件。

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

这可以用 jQuery 来完成,如果可以,怎么做?或者这应该用PHP或其他东西来完成吗?

回答by karim79

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

我可能会建议将此作为更优雅的降级解决方案,使用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>

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

即使没有 Javascript,至少这样用户会得到一些反馈。

回答by Rob

If you don't want search engines to index certain files, you can use robots.txtto tell web spiders not to access certain parts of your website.

如果您不希望搜索引擎索引某些文件,您可以使用robots.txt告诉网络蜘蛛不要访问您网站的某些部分。

If you rely only on javascript, then some users who browse without it won't be able to click your links.

如果您只依赖 javascript,那么一些没有它浏览的用户将无法点击您的链接。

回答by Kobi

Here's a nice article that shows many ways of hiding files from search engines:

这是一篇不错的文章,展示了从搜索引擎隐藏文件的多种方法:

http://antezeta.com/news/avoid-search-engine-indexing

http://antezeta.com/news/avoid-search-engine-indexing

JavaScript isn't a good way not to index a page; it won't prevent users from linking directly to your files (and thus revealing it to crawlers), and as Rob mentioned, wouldn't work for all users.
An easy fix is to add the rel="nofollow"attribute, though again, it's not complete without robots.txt.

JavaScript 不是不索引页面的好方法;它不会阻止用户直接链接到您的文件(从而将其透露给抓取工具),并且正如 Rob 所提到的,不适用于所有用户。
一个简单的解决方法是添加rel="nofollow"属性,但同样,如果没有 robots.txt,它是不完整的。

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

回答by MacAnthony

Yes, you would have to change the window.location.href to the url of the file you would want to download.

是的,您必须将 window.location.href 更改为要下载的文件的 url。

window.location.href = 'http://www.com/path/to/file';

回答by EL missaoui habib

 var link=document.createElement('a');
 document.body.appendChild(link);
 link.href=url;
 link.click();

回答by Ganganath Rathnasekara

  • Using jQuery function

        var valFileDownloadPath = 'http//:'+'your url';
    
       window.open(valFileDownloadPath , '_blank');
    
  • 使用 jQuery 函数

        var valFileDownloadPath = 'http//:'+'your url';
    
       window.open(valFileDownloadPath , '_blank');
    

回答by Otvazhnii

By stating window.location.href = 'uploads/file.doc';you show where you store your files. You might of course use .htacess to force the required behaviour for stored files, but this might not always be handful....

通过说明window.location.href = 'uploads/file.doc';您显示您存储文件的位置。您当然可以使用 .htacess 来强制存储文件所需的行为,但这可能并不总是少数......

It is better to create a server side php-file and place this content in it:

最好创建一个服务器端 php 文件并将此内容放入其中:

header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$_REQUEST['f']);
readfile('../some_folder/some_subfolder/'.$_REQUEST['f']); 
exit;

This code will return ANY file as a download without showing where you actually store it.

此代码将作为下载返回任何文件,而不会显示您实际存储它的位置。

You open this php-file via window.location.href = 'scripts/this_php_file.php?f=downloaded_file';

您通过以下方式打开此 php 文件 window.location.href = 'scripts/this_php_file.php?f=downloaded_file';

回答by ling

I suggest you use the mousedown event, which is called BEFORE the click event. That way, the browser handles the click event naturally, which avoids any code weirdness:

我建议您使用 mousedown 事件,该事件在 click 事件之前调用。这样,浏览器会自然地处理点击事件,从而避免任何代码怪异:

(function ($) {


    // with this solution, the browser handles the download link naturally (tested in chrome and firefox)
    $(document).ready(function () {

        var url = '/private/downloads/myfile123.pdf';
        $("a").on('mousedown', function () {
            $(this).attr("href", url);
        });

    });
})(jQuery);

回答by letronje

Hidden iframescan help

隐藏的 iframe可以提供帮助

回答by Russell Shingleton

See here for a similar post on using jQuery to clear forms: Resetting a multi-stage form with jQuery

有关使用 jQuery 清除表单的类似帖子,请参见此处:Resetting a multi-stage form with jQuery

You may also be running into an issue where the values are being repopulated by the struts value stack. In other words, you submit your form, do whatever in the action class, but do not clear the related field values in the action class. In this scenario the form would appear to maintain the values you previously submitted. If you are persisting these in some way, just null each field value in your action class after persisting and prior to returning SUCCESS.

您可能还会遇到一个问题,其中值被 struts 值堆栈重新填充。换句话说,您提交表单,在操作类中执行任何操作,但不清除操作类中的相关字段值。在这种情况下,表单看起来会保留您之前提交的值。如果您以某种方式持久化这些,只需在持久化之后和返回 SUCCESS 之前将操作类中的每个字段值设为 null。