【问题标题】:Mongodb GridFS having trouble downloading .ppt and .doc files?Mongodb GridFS 在下载 .ppt 和 .doc 文件时遇到问题?
【发布时间】:2013-01-14 07:08:27
【问题描述】:

我的控制器中有这个功能来下载我存储在我的 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 上。只有 pdf 文件运行良好。 谁能告诉我是什么问题?

【问题讨论】:

  • 尝试调用 $object->getSize 而不是使用 strlen 函数。

标签: php mongodb powerpoint mime-types gridfs


【解决方案1】:

好吧,经过一年和大量阅读、理解和实施 PHP,我能够回答我自己的问题,我已经使用 ob_clean() 方法修复了错误,它就像一个魅力,希望这对某人有所帮助未来……

        $content= $object->getBytes();
        header("Content-Length: " . strlen($content)); 
        header($mimeType); 
        ob_clean(); 
        echo($content);

【讨论】:

  • 您能告诉我为什么需要使用ob_clean 来清空缓冲区吗?
  • 如果我们使用ob_clean(); flush();
  • ob_clean 在渲染图像之前清除缓冲区,同时清除杂散空间..
  • 您的解决方案在同样的情况下帮助了我。赞成。
  • 通常这不应该发生,因为这意味着在脚本中的某个地方进行了早期(并且肯定是无意的)输出。您的解决方案帮助我发现,在我的情况下,不可见的输出是由于带有 UTF-8 字符的 UTF-8 文件引起的。
猜你喜欢
  • 2013-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-22
  • 2014-01-14
  • 1970-01-01
  • 1970-01-01
  • 2020-03-06
相关资源
最近更新 更多