2013-01-14 32 views
1

我有這個功能在我的控制器下載,我很存儲在我的MongoDB的網格文件:Mongodb GridFS無法下載.ppt和.doc文件?

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文件運行完美。 任何人都可以請告訴我什麼是問題?

+0

嘗試調用$對象 - > [的getSize(http://mongodb.github.com/node-mongodb-native/api-articles/nodekoarticle1.html)而不是使用strlen函數。 – WiredPrairie

回答

2

那麼,經過一年多的閱讀,理解和實現PHP我能夠回答我自己的問題,我已經修復了錯誤使用ob_clean()方法,它的工作就像一個魅力,希望這可以幫助某人在未來......

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

你能告訴我爲什麼需要使用'ob_clean'來清空緩衝區? – Slimshadddyyy

+0

另外,如果我們使用'ob_clean(); flush();' – Slimshadddyyy

+0

ob_clean清除緩衝區,然後你可以渲染圖像,也清除雜散空間。 – Aditya