Question

I am trying to download file in my codeigniter web application using javascript and codeigniter controller, it shows the file contents in ASCII format but not download the file directly

view.php

<a href="javascript:prd_download(this)">Download</a>

<img src="<?php echo site_url()."/../images/uploads/".$jobno."/thumb_".$prd_row->filename; ?>" alt="Loading Image..." >

<input type="checkbox" name="img_check" id="img_check" class="img_check" image="<?php echo $prd_row->filename ?>">


<script type="text/javascript">
function prd_download(ele)
{   
    var selected_images = $(".img_check:checked");

    var job_no = $("#product_table").attr("jobno");

    var image_name = new Array();

    for(i = 0; i < selected_images.length; i++)
    {
        image_name[i] = $(selected_images[i]).attr('image');
    }

        $.get('<?php echo site_url('project_controller/file_download') ?>', {file_name : image_name, jobno : job_no});
}
</script>

controller.php

function file_download()
    {
        $url_para = $_GET['file_name'];
        $job_no = $_GET['jobno'];
        $this->load->helper('download');
        $data = file_get_contents(site_url().'/../images/uploads/'.$job_no."/".$url_para[0]);
        $name = 'myphoto.jpg';

        force_download($name, $data);
}

I had tried by changing path also, but its not working...

Was it helpful?

Solution

I made changes in my javascript

<script type="text/javascript">
function prd_download(ele)
{   
    var selected_images = $(".img_check:checked");

    var job_no = $("#product_table").attr("jobno");

    var image_name = new Array();

    for(i = 0; i < selected_images.length; i++)
    {
        image_name[i] = $(selected_images[i]).attr('image');
    }

        window.location.href = "<?php echo site_url('project_controller/file_download') ?>?file_name="+ image_name +"&jobno="+ job_no;
}
</script>

In javascript I called controller directly, cause $.get function treat like call back...

Now its working fine...! :) :D

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top