【问题标题】:File downloading in AJAX with download counter使用下载计数器在 AJAX 中下载文件
【发布时间】:2015-12-11 13:38:50
【问题描述】:

我正在 AJAX 中创建简单的文件下载计数器。但在我的代码中,计数器在 PHP 代码中工作意味着每次下载它的值增加一,但文件没有下载。下面是我的代码

index.php

<script type="text/javascript">
$(function() {

$(".download_button").click(function() {         
    var test = $("#content").val();
    var dataString = 'content='+ test;  

    $.ajax({
        type: "POST",
        url: "download_counter.php",
        data: dataString,
        cache: false,
        success: function(html){      
               $("#display").after(html);             
               }
    });
   return false;
 });           
});
</script>
<a href="file_path/file.pdf" class="download_button" id="v" download>
    Download 
</a>

download_counter.php

<?php      
       //Code for counter Increment 
      //Query to Update database
?>

我对 PHP 代码中的数据库或下载计数器没有任何问题。我在下载文件时遇到问题。计数器加一,但文件未下载。

【问题讨论】:

  • 您必须在 click 事件处理程序中将 return false; 替换为 return true;
  • 谢谢@mapek...!它现在工作
  • 太棒了!我已将其添加为答案并链接到另一个问题的有用答案。

标签: php ajax


【解决方案1】:

您必须在 onclick 事件处理程序中将 return false; 替换为 return true;,以便执行超链接的默认事件。

请查看此答案以获取有关事件处理程序中return false; 的更多信息: https://stackoverflow.com/a/11184286/3647441

【讨论】:

    猜你喜欢
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    • 2014-12-28
    • 1970-01-01
    • 1970-01-01
    • 2013-08-27
    • 2016-01-06
    相关资源
    最近更新 更多