dqs13465424392 2016-03-21 08:26
浏览 47

我希望用户在更新codeigniter中数据库的下载计数器后下载文件

i am trying to set up a download counter for files. My plan is: When a user clicks on the download button, the id of that file is sent to the download controller, the download controller updates the download column by 1 with its unique id, and then user is redirected back to the view, and download starts. I have found a way to send the id of a particular file to the download controller when the download button is clicked. I used a form with hidden fields in the view to send the id. I checked online, and i saw many download counters but most of them update textfiles. I want a column in the database to be updated, not text files. Here is my download form view:

   <form id="form" action="<?php echo base_url();?>Download" method="post">
    <input type="hidden" placeholder="ID" name="id"  value ="<?php echo $res->id; ?>"  class="wow fadeInUp">
    <input type="hidden" placeholder="ID" name="url"  value ="<?php echo $res->url; ?> class="wow fadeInUp">
    <input type="submit" value="DOWNLOAD" id="download" class="wow fadeInUp">
    </form>

This view sends id and other data to the download controller, here is my download controller:

  function index()
  { 
     $id = $this->input->post("id");
     $url = $this->input->post("url");
     $this->home_model->update_downloads_column($id);
      redirect("Music/".$url);
  }

i need a way forward. Right now, the downloads column on the database has been updated, how do i now make download of that file start?

  • 写回答

1条回答 默认 最新

  • douzhong1907 2016-03-21 10:42
    关注

    Why not use ajax to pass value and the anchor tag in html that has download attribute?

    check that here :

    http://www.w3schools.com/tags/att_a_download.asp

    Code:

    <a href="/pathtofile/file.txt" id="download" download>
    

    And then just do something like :

    <script>
    $(document).ready(function(){
        $("#download").on( "click",function () {
            var id = $('input[name=id]').val();
            var url = $('input[name=url]').val();
           $.ajax({
                type: "POST",
                url: <?php echo base_url();?>+"Download",
                data: {id: id, url: url},
                 success: function(){
                    //if success do something
                    }
                 });
       });
    })
    </script>
    

    So when you click anchor, download will start and ajax will pass id and url to controller.

    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图