2012-01-16 79 views
2

我一直試圖在Zend MVC應用程序中使用JsPush Zend進度條,但尚未成功。我使用了github上的JsPush.php示例來創建一個獨立的進度條測試應用程序,但這不使用Zend MVC框架。當我使用JsPush示例程序中的CSS,HTML,JavaScript和PHP代碼時,我看到我的進度條出現(CSS工作),但在我的套接字寫入過程中沒有得到任何結果。我已經將JsPush CSS代碼逐字地用於鏈接到我的應用程序的css文件中,因此我不會在此重複這一點。下面是從我的MVC應用程序,該控制器代碼exerpts:

如何在Zend MVC應用程序中使用Zend ProgressBar

protected function _sendFile($job) 
{ 
    $fp = fopen($job->source_file, "rb"); 
    if($fp == null || $fileData['size'] == 0) { 
     $this->view->errorMessage = 'Error - file open failed for ' . $job->source_file; 
     return false; 
    } 

    $this->totalXmitSize = $fileSize; 
    $this->currentXmitSize = 0; 
    $progressMessage = ''; 

    $adapter = new Zend_ProgressBar_Adapter_JsPush(array 
     ('updateMethodName' => 'Zend_ProgressBar_Update', 
     'finishMethodName' => 'Zend_ProgressBar_Finish')); 
    $progressBar = new Zend_ProgressBar($adapter, 0, $this->totalXmitSize); 

    while(!feof($fp)) { 
     $blkSize = ($fileSize > 1024) ? 1024 : $fileSize; 
     $xmitBuf = fread($fp, $blkSize); 

     if($xmitBuf === false || !strlen($xmitBuf)) { 
      $this->view->errorMessage = 'Error - file open failed for ' . $job->source_file; 
      return false; 
     } 

     if($this->_writeData($xmitBuf, $blkSize) == false) { 
      return false; 
     } 

     $fileSize -= $blkSize; 
     $this->currentXmitSize += $blkSize; 
     $progress = $this->_calculateXmitProgress(); 

     if($progress < 25) { 
      $progressMessage = 'Just beginning'; 
     } elseif($progress < 50) { 
      $progressMessage = 'less than half done'; 
     } elseif($progress < 75) { 
      $progressMessage = 'more than half done'; 
     } else { 
      $progressMessage = 'almost done'; 
     } 

     $progressBar->update($this->currentXmitSize, $progressMessage); 

     if($fileSize <= 0) { 
      break; 
     } 
    } 
    fclose($fp); 
    $xmitBuf = ''; 
    $bufLen = 0; 

    if($this->_readData($xmitBuf, $bufLen) === false) { 
     return false; 
    } 

    $progressBar->finish(); 
    if(strncmp($xmitBuf, MSG_STATUS_OK, strlen(MSG_STATUS_OK)) != 0) { 
     $this->view->errorMessage = 'Error - invalid response after file transfer complete'; 
     return false; 
    } 

    return true; 
} 

視圖代碼:

<?php 
$this->title = "My Title"; 
$this->headTitle($this->title); 
$this->layout()->bodyScripts = 'onload="startProgress();"'; 
?> 
<p class="errors"> <?php echo $this->errorMessage ?> </p > 
<?php echo $this->form->setAction($this->url()); 
?> 

<div id="progressbar"> 
    <div class="pg-progressbar"> 
     <div class="pg-progress" id="pg-percent"> 
      <div class="pg-progressstyle"></div> 
      <div class="pg-invertedtext" id="pg-text-1"></div> 
     </div> 
     <div class="pg-text" id="pg-text-2"></div> 
    </div> 
</div> 
<div id="progressBar"><div id="progressDone"></div></div> 

的layout.phtml代碼:

function startProgress() 
{ 
    var iFrame = document.createElement('iframe'); 
    document.getElementsByTagName('body')[0].appendChild(iFrame); 
    iFrame.src = 'MyController.php'; 
} 

function Zend_ProgressBar_Update(data) 
{ 
    document.getElementById('pg-percent').style.width = data.percent + '%'; 
    document.getElementById('pg-text-1').innerHTML = data.text; 
    document.getElementById('pg-text-2').innerHTML = data.text; 
} 

function Zend_ProgressBar_Finish() 
{ 
    document.getElementById('pg-percent').style.width = '100%'; 
    document.getElementById('pg-text-1').innerHTML = 'done'; 
    document.getElementById('pg-text-2').innerHTML = 'done'; 
} 
</script> 

</head> 
<body <?php echo $this->layout()->bodyScripts ?>> 
<div> 
    <h1><?php echo $this->escape($this->title); ?></h1> 
    <?php echo $this->layout()->content; ?> 
</div> 

當這個運行我得到了頁面源碼中的大量JavaScript,但沒有進展r esults:

<script type="text/javascript">parent.Zend_ProgressBar_Update({"current":0,"max":3397,"percent":0,"timeTaken":0,"timeRemaining":null,"text":null});</script><br /> 
<script type="text/javascript">parent.Zend_ProgressBar_Update({"current":1024,"max":3397,"percent":30.14424492199,"timeTaken":0,"timeRemaining":0,"text":"less than half done"});</script><br />                    
<script type="text/javascript">parent.Zend_ProgressBar_Update({"current":2048,"max":3397,"percent":60.28848984398,"timeTaken":0,"timeRemaining":0,"text":"more than half done"});</script><br /> 
<script type="text/javascript">parent.Zend_ProgressBar_Update({"current":3072,"max":3397,"percent":90.43273476597,"timeTaken":0,"timeRemaining":0,"text":"almost done"});</script><br /> 
<script type="text/javascript">parent.Zend_ProgressBar_Update({"current":3397,"max":3397,"percent":100,"timeTaken":0,"timeRemaining":0,"text":"almost done"});</script><br /> 
<script type="text/javascript">parent.Zend_ProgressBar_Finish();</script><br /> 

這一切的JavaScript推我出去形式的視圖。我花了好幾天在Zend文檔站點上搜索網頁,找到在MVC框架應用程序中成功使用Zend ProgressBar的人的例子,但是找不到任何工作示例。預先感謝您的幫助。

回答

0

那麼我已經成功獲得JsPush進度條,在某種程度上在某種程度上,但對我的應用程序來說不是100%正確的。

進度條工作,但表單結果最終在隱藏的iframe中。沒有辦法到達該iframe上的提交按鈕,因此我的用戶必須使用後退按鈕才能跳出該表單。

獲取進度條工作的唯一方法是使用表單中的目標屬性並將其分配給iframe的name屬性。如果我不使用目標屬性,則表單將滾動爲progressBar->update;的每次調用。所以我的表單結果最終在父頁面上滾動了幾百行,滾動條從不起作用。

我試過使用Zend文檔中的例子,但它不起作用。這是我對Zend框架的第一次冒險,它令人非常沮喪和失望。對28天前我原來的問題沒有一個迴應。

0

我有同樣的問題。對我來說,它工作,因爲我做了以下步驟。 轉到php.ini並檢查是否在您的php.ini中設置了以下數據。

  • output_buffering =關
  • ; output_handler =
  • 要用zlib.output_compression =關
  • ; zlib.output_handler =

然後去上課Zend_ProgressBar_Adapter_JsPush並找到方法 - 這是在底部 - _outputData。刪除此方法中的所有內容並輸入

// 1024 padding is required for Safari, while 256 padding is required 
    // for Internet Explorer. The <br /> is required so Safari actually 
    // executes the <script /> 
    echo str_pad($data . '', 1024, ' ', STR_PAD_RIGHT) . "\n"; 

    echo(str_repeat(' ',256)); 
    if (ob_get_length()){ 
     @ob_flush(); 
     @flush(); 
     @ob_end_flush(); 
    } 
    @ob_start(); 
相關問題