2014-01-08 64 views
2

我試圖下載文件中使用JavaScript和笨控制器我的笨web應用程序,它顯示了ASCII格式的文件內容,但無法下載文件直接如何在codeigniter中下載文件?

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

'site_url()。'/ ../images/uploads /'似乎無效。嘗試從'/ images/uploads/..開始'' – Bora

+0

檢查是否有任何控制檯錯誤? – Arunu

+0

當我使用site_url()。'/ ../images/uploads /並在變量中打印這個變量值時,儘量使用'base_url()'而不是'site_url()' –

回答

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