如何在codeigniter中下载文件?

时间:2014-01-08 12:19:23

标签: php codeigniter download codeigniter-2 force-download

我正在尝试使用javascript和codeigniter控制器在我的codeigniter Web应用程序中下载文件,它以ASCII格式显示文件内容但不直接下载文件

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

我曾尝试过改变路径,但它不起作用......

1 个答案:

答案 0 :(得分:1)

我在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>

在javascript中我直接调用了控制器,导致$.get函数处理就像回调...

现在它的工作正常......! :):D