简体   繁体   English

在调用下载功能之前调用ajax函数

[英]call ajax function before calling function of download

i am developing web site in codeigniter and there is a facility of downloading file. 我正在开发codeigniter的网站,并有下载文件的设施。 my problem is that i wants to keep a record that when a user download any document it insert the record in database to keep the download history. 我的问题是我想保留一个记录,当用户下载任何文件时,它会在数据库中插入记录以保存下载历史记录。 i have called jquery ajax on that button click and in href attribute the function of download. 我已经在该按钮上调用了jquery ajax,并在href属性中调用了下载功能。 please give me suggestions that how to first call jquery function when i click the download function and after it the download is done. 请给我一些建议,当我点击下载功能时,如何首先调用jquery函数,然后完成下载。 thank you in advance 先感谢您

this is my code of download 这是我的下载代码

public function download_b_question_paper($fileName = NULL) 
{   
    $this->load->helper('download'); //load helper
    if ($fileName)
    {
        $file = realpath ( "b_question" ) . "\\" . $fileName;               
        if (file_exists ( $file ))// check file exists    
        {                   
            $data = file_get_contents( $file );// get file content
            force_download ( $fileName, $data );//force download
        }
    }
}

this is in the view to create links to download using ajax 这是为了创建使用ajax下载的链接

$(".link").click(function(){
    var id=$(this).data("data1");
    $.ajax({
        type : 'POST',
        url  : '<?php echo base_url();?>index.php/Faculty_controller/get_all_bmaterial',
        data : { 'id' : id },
        success : function(data)
        {
            $(".tbl").html("");
            recs=$.parseJSON(data);
            head='<tr><td>Sr. No</td><td align=center><br><h4>Material Name</td><td colspan="2"><center>Action</center></td></tr>';
            $(".tbl").append(head);
            $.each(recs,function(i,v)
            {
                a=i+1;
                r='<tr><td><br><center>'+a+'</td><td><br>'+v.file_name+'</td><td><br><center><a class="dwnld btn btnd btn-danger" href="<?php echo base_url();?>index.php/Student_controller/download_bachelor_material/'+v.file_name+'"/"'+v.materialno+'" data-data1='+v.materialno+' data-data2='+v.file_name+' data-content="Download this material" data-placement="bottom" data-trigger="hover"><i class="fa fa-download"></i></a></td></tr>';
                $(".tbl").append(r);
                $('a').tooltip({ });                        
            });//end of foreach
        }//end of success 
    });//end of ajax
});//end of link click function

this is the ajax i wants to call before download function is called 这是我想在调用下载函数之前调用的ajax

$("a").click(function(){
    alert('hello');
    $.ajax({    
        type : 'POST',
        data : { 'subkey' :  id },
        url : "<?php echo base_url()?>index.php/Studnet_Controller/b_mat_activity",
        success : function(data)
        {
            alert('hello');
        }
    });
});

In you ajax config, there is a callback called beforeSend you can use that to execute function before you send the request. 在你的ajax配置中,有一个名为beforeSend的回调你可以在发送请求之前使用它来执行函数。

$ajax({
    /*...*/
    beforeSend: function(xhr) {
        //beforeSend;
    },
    success: function(data) {

    },
    error: function(error) {

    }
})

You could also call the second call in the success of the first one. 您也可以在第一个呼叫中成功呼叫第二个呼叫。

$ajax({
    /*...*/
    success: function(data) {
    //secondCall
        $.ajax({
             /*...*/
        })
    },
    error: function(error) {

    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM