2016-06-21 42 views
1

我有簡單的XMLHttpRequest到php服務器腳本。無法用XMLHttpRequest讀取大pdf文件

fxhr.onload = function (data) { 
    //console.log(JSON.parse(data.target.response)); 
    var string = JSON.parse(data.target.response).data.content; 
    console.log(JSON.parse(data.target.response)); 
    DEFAULT_URL = convertDataURIToBinary(string); 

    var config = getViewerConfiguration(); 
    window.PDFViewerApplication = pdfjsWebLibs.pdfjsWebApp.PDFViewerApplication; 
    pdfjsWebLibs.pdfjsWebApp.PDFViewerApplication.run(config); 
    }; 
    fxhr.open('POST', window.location.pathname + 'content'); 

服務器的腳本代碼:

 public function execute() 
{ 
    $productModel = new shopProductModel(); 
    $bookModel = new shopEbooksPluginItemModel(); 
    $item = $productModel->getByField('url', waRequest::param('product_code')); 
    $bookFile = $bookModel->getByField(array(
    'product_id' => $item['id'], 
    'book_type' => waRequest::param('reader_state') 
), false); 
    $content = @file_get_contents($this->getBookFullPath($bookFile)); 

    $this->response = array('content' => $this->getBaseEncodedFile()); 
} 

private function getBookFullPath($bookFileRow) 
{ 
    return $bookFileRow['file_path'] . $bookFileRow['file_name']; 
} 

private function getBaseEncodedFile($content) 
{ 
    return 'data:application/pdf;base64,' . base64_encode($content); 
} 

,如果我嘗試讀一些輕鬆的PDF文件,它的工作好,但如果我嘗試讀取40MB的PDF文件服務器響應只有「數據:應用程序/ PDF格式;的base64 「。 file_exists返回true。

+0

'error_log'中是否有消息可能有幫助? –

+0

嗯,也許我試着檢查 –

回答

0

PHP在php.ini文件中有幾個設置可能導致您的問題。

memory_limit - 這幾乎是肯定的問題; PHP必須將所有數據加載到內存中才能處理它。嘗試將此值提高至比預期處理的最大文件大50%。

max_execution_time - 如果腳本花費很長時間處理所有數據,它將被終止。嘗試增加此值。

我經常不得不修改這些限制在php.ini允許腳本處理大量的數據。

+0

'memory_limit 128' 'max_execution_time 180' 立即返回null –

+0

'memory_limit 128'表示128 **字節**。嘗試'memory_limit 128M'爲128 **兆字節** –

+0

是的128M現在在php.ini –