관리자가 업로드 한 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 파일을 다운로드하려고 할 때 나는 이런 종류의 메시지를 받는다.
감사합니다.
Ajax를 통해 파일을 다운로드해야합니까? 링크를'downloadfiles' url로 설정하면 파일을 다운로드하고 같은 페이지에 머물러있게됩니다. –
force_download를 시도 했습니까? –
@KraneBird -이 작업을 수행하는 데 올바른 방법을 제공해 주시겠습니까? 여전히 나는 그것을 올바르게 할 수 없었다. 감사합니다. – Yasitha