我正在尝试制作一个可以下载由管理员上传的 jpg、png、pdf、docx 文件的网站。
上传这些内容没有问题。上传时,我也将该文件名插入到 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
}
}
?>
这是我的ajax代码
function downloadFile(str) {
//alert(str);
$.ajax({
type: 'POST',
url: 'downloadfiles',
data: {value: str},
success: function(data) {
alert('ok' + data);
}
});
}
当有人单击下载按钮时,使用 ajax 和 jquery 我将该文件名发送到 download_controller。这是 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 文件时,我收到了这种消息。
有人可以告诉我我在这里做错了什么吗?
谢谢你