我在我的控制器中有这个功能来下载我存储在我的mongodb网格中的文件:
function download_presentation($ext,$store_filename)
{
$grid = $this->mongo->db->getGridFS();
//query the file object
$objects = $grid->find();
//set content-type header, output in browser
switch ($ext) {
case 'pdf':
$mimeType = 'Content-type: application/pdf';
break;
case 'jpg':
$mimeType = 'Content-type: image/jpg';
break;
case 'png':
$mimeType = 'Content-type: image/png';
break;
case 'doc':
$mimeType = 'Content-type: application/msword';
break;
case 'docx':
$mimeType = 'Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document';
break;
case 'xls':
$mimeType = 'Content-type: application/vnd.ms-excel';
break;
case 'xlsx':
$mimeType = 'Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
break;
case 'ppt':
$mimeType = 'Content-type: application/x-mspowerpoint';
break;
case 'pptx': $mimeType = 'Content-type: application/vnd.openxmlformats- officedocument.presentationml.presentation';
break;
default:
$mimeType='Content-type: application/pdf';
}
while($object = $objects->getNext()) :
if(($object->file['filename'])==$store_filename){
$content=$object->getBytes();
header("Content-Length: " . strlen($content));
header($mimeType);
echo ($content);}
endwhile;
} 每当我下载.ppt文件时,它都说PowerPoint无法打开此文件,因为它不是.ppt。对于pptx文件,当我在下载后打开它时,它正在工作,当我点击从PowerPoint弹出的“修复”时。事情发生在.doc / MS-Word.Only pdf文件运行完美。 谁能告诉我这是什么问题?
答案 0 :(得分:2)
好吧,经过一年多的阅读,理解和实现PHP,我能够回答我自己的问题,我使用ob_clean()
方法修复了错误,它就像一个魅力,希望这有助于将来有人......
$content= $object->getBytes();
header("Content-Length: " . strlen($content));
header($mimeType);
ob_clean();
echo($content);