2011-12-12 70 views
0

我通過AJAX ajaxFileUpload插件上傳圖像文件,該插件使用iframe提交文件。我已經成功地將文件上傳到我的控制器,並且我可以看到tmp_name,name,error = 0等,但是當我使用這個$ this-> data ['Card'] ['tmp_name']和move_uploaded_file時,它始終返回false的路徑是正確的......我不確定從這一點起。在Cakephp中上傳AJAX文件輸入圖像

下面是到目前爲止我的代碼爲視圖文件...

function ajaxFileUpload() { 
    $.ajaxFileUpload({ 
     url: '/cards/ajaxFrontCardUpload', 
     secureuri: false, 
     fileElementId: 'CardUploadFront', 
     dataType: 'json', 
     success: function (data, status) { 
      console.log(data); 
      $('#uploadFrontImage').attr('src', data.tmp_path); 
     }, 
     error: function (data, status, e) { 
      alert(e); 
     } 
    }) 
    return false; 
} 

$('#CardUploadFront').live('change', function() { 
    ajaxFileUpload(); 
}); 

echo $form->file('Card.uploadFront', array('class'=>'file')); 

下面是控制器功能:

public function ajaxFrontCardUpload() { 
     $this->layout = 'ajax'; 
     $tmp_name = $this->data['Card']['uploadFront']['tmp_name']; 
     $tmp_name = $this->data['Card']['uploadFront']['tmp_name'].'/'.$this->data['Card']['uploadFront']['name']; 
     $json_response['tmp_path'] = '/img/cards/temp/'.time().'.png'; 
     if(move_uploaded_file($tmp_name, $json_response['tmp_path'])){ 
      $json_response['response'] = 'true'; 
     }else{ 
      $json_response['response'] = 'false'; 
     } 
     $this->set(compact('json_response')); 
    } 

任何想法的傢伙?

回答

1

的問題是在這裏:

public function ajaxFrontCardUpload() { 
     $this->layout = 'ajax'; 
     $tmp_name = $this->data['Card']['uploadFront']['tmp_name']; 
     $tmp_name = $this->data['Card']['uploadFront']['tmp_name'].'/'.$this->data['Card']['uploadFront']['name']; 
//notice here that $tmp_name now no longer references the path to the uploaded file 
     $json_response['tmp_path'] = '/img/cards/temp/'.time().'.png'; 
     if(move_uploaded_file($tmp_name, $json_response['tmp_path'])){ 
      $json_response['response'] = 'true'; 
     }else{ 
      $json_response['response'] = 'false'; 
     } 
     $this->set(compact('json_response')); 
    } 

到上傳文件的路徑存儲在$this->data['Card']['uploadFrom']['tmp_name']。 當你附加'/'.$this->data['Card']['uploadFront']['name']時,你的$tmp_name變量不再指向上傳的文件。這就是爲什麼move_uploaded_file返回false。