使用jQuery下载文件


                    

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

例如,代替:</ p>

  在此处下载
</ code> </ pre>

我可以使用:</ p>

  在此处下载

  $('a')。click ... //一些jquery下载文件
</ code> </ pre>

这样,Google不会为我的HREF和私人文件建立索引。</ p>

这可以用jQuery完成吗,如果可以,怎么办? 还是应该使用PHP或其他方式完成?</ p>
     </ div>

展开原文

原文

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.

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

10个回答


我可能会建议使用 < code> preventDefault </ code> :</ p>

  $('a')。click(function(e){
     e.preventDefault(); //停止浏览器跟踪
     window.location.href ='上传/file.doc';
});

立即下载!
</ code> </ pre>

即使没有JavaScript,至少用户也可以通过这种方式获得反馈。</ p>
     </ div>

展开原文

原文

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>

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


这是一篇很好的文章,显示了从搜索引擎隐藏文件的多种方法:</ p>


  

http://antezeta.com/news/avoid-search-engine-indexing < / a> </ p>
</ blockquote>

JavaScript不是不索引页面的好方法。 它不会阻止用户直接链接到您的文件(从而将其显示给搜寻器),而且正如罗布提到的那样,并非对所有用户都有效。

一个简单的解决方法是添加 rel =“ nofollow” </ code>属性,尽管同样,如果没有robots.txt,它是不完整的。</ p>

  在此处下载
</ code> </ pre>
     </ div>

展开原文

原文

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

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.

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

展开原文

原文

Hidden iframes can help


通过说明 window.location.href ='uploads / file.doc'; </ code>来显示存储文件的位置。 您当然可以使用.htacess强制执行存储文件的必需行为,但这可能并不总是很简单.... </ p>

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

  header('Content-Type:application / octet-stream');
header('Content-Disposition:附件; filename ='。$ _ REQUEST ['f']));
readfile('../ some_folder / some_subfolder /'.$_ REQUEST ['f']);
出口;
</ code> </ pre>

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

您通过 window.location.href ='scripts / this_php_file.php?f = downloaded_file'打开此php文件; </ code> </ p>
     </ div>

展开原文

原文

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....

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

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';


我建议您使用mousedown事件,该事件在click事件之前被称为。
这样,浏览器可以自然地处理click事件,从而避免任何代码怪异:</ p>

 (function($){

     //使用此解决方案,浏览器可以自然处理下载链接(已在chrome和firefox中测试)
     $(document).ready(function(){

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

     });
})(jQuery);
</ code> </ pre>
     </ div>

展开原文

原文

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:

(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);


是的,您必须将window.location.href更改为要下载的文件的URL。</ p>

  window.location.href ='http://www.com/path/to/file';
</ code> </ pre>
     </ div>

展开原文

原文

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

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



  • 使用jQuery函数</ p>

      var valFileDownloadPath ='http //:'+'您的url';
    
    

        window.open(valFileDownloadPath,'_blank');
    </ code> </ pre> </ li>
    </ ul>
         </ div>

展开原文

原文

  • Using jQuery function

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


  var link = document.createElement('a');
  document.body.appendChild(link);
  link.href = url;
  link.click();
</ code> </ pre>
     </ div>

展开原文

原文

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


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

如果您仅依靠javascript,那么一些没有使用javascript进行浏览的用户将无法单击您的链接。</ p>
     </ div>

展开原文

原文

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

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


有关使用jQuery清除表单的类似信息,请参见此处:重置多个 jQuery的舞台表单 </ p>

您还可能会遇到一个问题,其中Struts值堆栈重新填充了这些值。 换句话说,您提交表单,在操作类中执行任何操作,但不要清除操作类中的相关字段值。 在这种情况下,该表单似乎将保留您先前提交的值。 如果要以某种方式保留这些值,则在保留之后并返回SUCCESS之前,只需将操作类中的每个字段值都设为空即可。</ p>
     </ div>

展开原文

原文

See here for a similar post on using jQuery to clear forms: 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.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问