2009-10-16 98 views
0

我正在努力上傳文件所需的一點dojo。現在,文件上傳表單位於dojo對話框中,因此在用戶選擇「上傳文件」按鈕之前隱藏。使用zend框架問題的dojo文件上傳

該按鈕可以在網站上的任何地方點擊,所以我創建了一個控制器來處理上傳。

目前我只是想獲得它的工作,並在我的頭上腳本我有以下幾點:

<?php $this->headScript()->captureStart(); ?> 
function sendForm(){ 

    //Hide the file input field 
    dojo.style('inputField',"display","none"); 

    //Show the progress bar 
    dojo.style('progressField',"display","inline"); 
    dojo.byId('preamble').innerHTML = "Uploading ..."; 

    dojo.io.iframe.send({ 
     url: "<?php echo $this->baseUrl(); ?>/fileprocssing/loadfile/", 
     method: "post", 
     handleAs: "text", 
     form: dojo.byId('StartFrm'), 
     handle: function(data,ioArgs){ 
      var fileData = dojo.fromJson(data);     
      if (fileData.status == "success"){ 
       //Show the file input field 
       dojo.style(dojo.byId('inputField'),"display","inline"); 
       dojo.byId('fileInput').value = ''; 

       //Hide the progress bar 
       dojo.style(dojo.byId('progressField'),"display","none"); 
       dojo.byId('uploadedFiles').innerHTML += "success: File: " + fileData.details.name 
        + " size: " + fileData.details.size +"<br>"; 
       dojo.byId('preamble').innerHTML = "File to Upload: "; 
      }else{ 
       dojo.style(dojo.byId('inputField'),"display","inline"); 
       dojo.style(dojo.byId('progressField'),"display","none"); 
       dojo.byId('preamble').innerHTML = "Error, try again: "; 
      } 
     } 
    }); 
} 



<?php $this->headScript()->captureEnd() ?> 

有了基本的上傳像這樣

<form id="StartFrm" enctype="multipart/form-data" 
     name="cvupload" 
     action="<?php echo $this->baseUrl();?>/fileprocssing/loadfile/" 
     method="post"> 

    <input type="hidden" name="MAX_FILE_SIZE" value="500000"> 
    <!-- wrapping these in spans to be able to modify 
     parts of this form depending on what the 
     dojo.io.iframe.submit() does --> 

    <span id="preamble">File to Upload:</span><br> 
    <span id="inputField"> 
     <input type="file" id="fileInput" name="uploadFile"> 
    </span> 

    <span id="progressField" style="display:none;"> 
     <div dojoType="dijit.ProgressBar" style="width:200px" indeterminate="true"></div> 
    </span> 

    <br/> 

    <button value="upload" dojoType="dijit.form.Button" 
      onclick="sendForm()">Upload</button> 
</form> 

什麼我想知道的是,如果從/ somecontroller/someaction/??調用窗體,我可以從/ fileprocssing/loadfile /中獲取包含上載數據信息的JSON數據對象。當文件被自動處理重定向到類似/ fileprocesing/reviewdata/

目前,我有這個樣子的

public function loadfileAction() { 

    $log = Zend_Registry::getInstance()->get('log'); 

    $log->log('in loadfileaction', Zend_Log::DEBUG); 

    $log->log($_FILES['uploadFile']['name'], Zend_Log::DEBUG); 

    $uploadedFile = array(
     'details' => $_FILES['uploadFile'], 
     'status' => 'success' 
    ); 


    $log->log($fileUploadData->toJson(), Zend_Log::DEBUG); 

    $foo = "{'status':'success',details: {name:'". 
$_FILES['uploadFile']['name']. 
"',size:". 
$_FILES['uploadFile']['size']. 
"}}"; 

    $log->log($foo, Zend_Log::DEBUG); 

    $this->view->fileData = $foo;   


} 

我手工暫時JSON數據的動作但會使用Zend_Dojo_Data,但目前我只是試圖讓這個工作。

我必須承認,我不知道道場那麼好,但試圖在最短的時間內讓我的頭靠近它。

在此先感謝。

回答

2

dojo.io.iframe.send要求將響應數據包裝在TEXTAREA標籤中。這是成功訪問和加載響應數據的唯一/最簡單的跨瀏覽器方式,並且是一項要求。它看起來像從操作中發回純JSON。

您也可以調整你的handleAs是「JSON」,跳過中間dojo.fromJson(數據)調用,它會被傳遞給你作爲一個JSON對象(提供的響應被包裹在上述TEXTAREA)

+0

感謝你...一直在煩我... – 2009-11-04 23:00:33