2014-03-06 17 views
0

관리자가 업로드 한 jpg, png, pdf, docx 파일을 다운로드 할 수있는 웹 사이트를 만들려고합니다.codeigniter를 사용하여 기존 파일을 다운로드하십시오.

이 콘텐츠를 업로드하는 데 문제가 없습니다. 업로드 할 때 그 파일 이름을 mysql 테이블에도 삽입한다. 그 테이블을 사용하여 업로드 된 파일 이름을 사용자에게 보여줍니다.

이것은 해당 파일을 표시하는 데 사용하는 코드입니다.

<?php 
      if (!empty($downloads)) { 
       foreach ($downloads as $val) { 
        ?>   <div class="col-md-3 col-sm-6 col-xs-12"> 
         <div class="panel panel-danger"> 
          <div class="panel-heading panel-title"> 
           <?php echo $val['upload_description']; ?> 
          </div> 
          <div class="panel-body"> 
           <?php echo $val['file_name']; ?> 
           <div class="clear-fix clear clearfix"></div> 
           <div id="download" onclick="downloadFile('<?php echo $val['file_name']; ?>');" class="btn btn-danger">Download</div> 
          </div> 
         </div> 
        </div> 
        <?php 
       } 
      } 
      ?> 

는이 내 아약스 코드

function downloadFile(str) { 
         //alert(str); 
         $.ajax({ 
          type: 'POST', 
          url: 'downloadfiles', 
          data: {value: str}, 
          success: function(data) { 
           alert('ok' + data); 
          } 
         }); 
        } 

때 아약스와 download_controller 해당 파일 이름을 전송 JQuery와 I'am를 사용하여 다운로드 버튼을 몇 번의 클릭. 이 결코 시작하지 않는 download_controller 파일

public function download_files() { 
    $fileName = $this->input->post('value'); 
    $file_path = 'uploaded/' . $fileName; 
    $this->_push_file($file_path, $fileName); 
} 

function _push_file($path, $name) { 
     $this->load->helper('download'); 
     // make sure it's a file before doing anything! 
     if (is_file($path)) { 
      // required for IE 
      if (ini_get('zlib.output_compression')) { 
       ini_set('zlib.output_compression', 'Off'); 
      } 

      // get the file mime type using the file extension 
      $this->load->helper('file'); 
      $mime = get_mime_by_extension($path); 
      // Build the headers to push out the file properly. 
      header('Pragma: public');  // required 
      header('Expires: 0');   // no cache 
      header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
      header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($path)) . ' GMT'); 
      header('Cache-Control: private', false); 
      header('Content-Type: ' . $mime); // Add the mime type from Code igniter. 
      header('Content-Disposition: attachment; filename="' . basename($name) . '"'); // Add the file name 
      header('Content-Transfer-Encoding: binary'); 
      header('Content-Length: ' . filesize($path)); // provide file size 
      header('Connection: close'); 
      readfile($path); // push it out 
      exit(); 
     } 
    } 

만 다운로드의 기능입니다. PNG 파일을 다운로드하려고 할 때 나는 이런 종류의 메시지를 받는다.

enter image description here

은 어떤 하나 I'am 여기서 뭐 잘못된 것이 무엇인지 말해 줄 수?

감사합니다.

+1

Ajax를 통해 파일을 다운로드해야합니까? 링크를'downloadfiles' url로 설정하면 파일을 다운로드하고 같은 페이지에 머물러있게됩니다. –

+1

force_download를 시도 했습니까? –

+0

@KraneBird -이 작업을 수행하는 데 올바른 방법을 제공해 주시겠습니까? 여전히 나는 그것을 올바르게 할 수 없었다. 감사합니다. – Yasitha

답변

1

내가 잘못 한 것을 발견했습니다. 덕분에

내가이이

$.ajax({ 
          type: 'POST', 
          url: 'downloadfiles', 
          data: {value: str}, 
          success: function(data) { 
           alert('ok' + data); 
          } 
         }); 

에서 내 아약스 코드를 변경 Mr.KraneBird합니다.

window.location.href = "<?php echo base_url().'downloadfiles/'?>" + str; 

download_controller에 게시하지 않고 지금 download_controller로 리디렉션합니다.

이제 오류없이 올바르게 작동합니다.

나를 도울 귀중한 시간을 낭비하는 사람마다 감사합니다.

+1

아약스 자체로 더 사용자에게 친숙하게 할 수 있습니다. 리디렉션하지 않고도 의미가 있습니다. 과 같은 컨트롤러 파일의 정확한 경로를 제공해야 할 수 있습니다. ' '.. 그냥 시도해보세요. –