2012-08-03 4 views
0

상태 : ajax 업로드 파일 필드가있는 양식. Q1 : ajax를 통해 이미지를 삭제 한 후 CMultiFileUpload 위젯을 표시하는 방법은 무엇입니까?img를 삭제하고 Ajax를 통해 CMultiFileUpload를 표시하는 방법

Q2 : 이미지를 즉시 업로드 한 후 이미지를 표시하는 방법은 무엇입니까?

내 모델 아무것도 변화

내보기

<div class="form"> 

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'fa-depreciation-form', 
    'enableAjaxValidation'=>false, 
    'htmlOptions' => array('enctype' => 'multipart/form-data'), 
)); ?> 
<div class="row" id="file_upload"> 
    <?php 
     if ($model->file_name){ 
      $file = '/images/'. $model->id . '/'. $model->file_name; 
      echo '<div id="forAjaxRefresh">'; 
      echo '<img src="http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . $file.'" width="150px" />'; 
      echo "<br />"; 
      echo CHtml::ajaxLink('remove', array('delimg', 'id'=>$model->id), array('update'=>'#forAjaxRefresh')); 
      echo '</div>'; 

     }else{  
      $this->widget('CMultiFileUpload', array(
       'model'=>$model, 
       'name'=>'file_name', 
       'attribute'=>'file_name', 
       'accept'=>'jpg|gif|png', 
       'options'=>array(
        //'onFileSelect'=>'function(e, v, m){ alert("onFileSelect - "+v) }', 
        //'afterFileSelect'=>'function(e, v, m){ alert("afterFileSelect - "+v) }', 
        //'onFileAppend'=>'function(e, v, m){ alert("onFileAppend - "+v) }', 
        //'afterFileAppend'=>'function(e, v, m){ alert("afterFileAppend - "+v) }', 
        'onFileRemove'=>'function(e, v, m){ alert("onFileRemove - "+v) }', 
        //'afterFileRemove'=>'function(e, v, m){ alert("afterFileRemove - "+v) }', 
        'max'=>3, 
       ), 
      )); 
     } 
     ?> 
    </div>  
<div class="row buttons"> 
     <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?> 
    </div> 

<?php $this->endWidget(); ?> 

</div><!-- form --> 

내 컨트롤러

public function upload($id) 
    { 
     // THIS is how you capture those uploaded images: remember that in your CMultiFile widget, you set 'name' => 'images' 
     $images = CUploadedFile::getInstancesByName('file_name'); 

     // proceed if the images have been set 
     if (isset($images) && count($images) > 0) { 

      // make the directory to store the pic: 
      if(!is_dir(Yii::getPathOfAlias('webroot').'/images/fadepreciation/'. $id)) { 
       mkdir(Yii::getPathOfAlias('webroot').'/images/fadepreciation/'. $id); 
       chmod(Yii::getPathOfAlias('webroot').'/images/fadepreciation/'. $id, 0755); 
       // the default implementation makes it under 777 permission, which you could possibly change recursively before deployment, but here's less of a headache in case you don't 
      } 

      // go through each uploaded image 
      foreach ($images as $image => $pic) { 
       $pic->saveAs(Yii::getPathOfAlias('webroot').'/images/tmp/' . $pic->name); 
       echo '<img src="'. Yii::getPathOfAlias('webroot').'/images/tmp/'.$id . '/' . $pic->name.' /><br />'; 
       if ($pic->saveAs(Yii::getPathOfAlias('webroot').'/images/fadepreciation/'.$id . "/" . $pic->name)) { 
        // add it to the main model now 
        $model=$this->loadModel($id); 
        $model->file_name = $pic->name; //it might be $img_add->name for you, filename is just what I chose to call it in my model 

        $model->save(); // DONE 
        unlink(Yii::getPathOfAlias('webroot').'/images/tmp/' . $pic->name); 
       } 
      } 
     } 
    } 

    /** 
    * Creates a new model. 
    * If creation is successful, the browser will be redirected to the 'view' page. 
    */ 
    public function actionCreate() 
    { 
     $model=new FaDepreciation; 

     if(isset($_POST['FaDepreciation'])) 
     { 
      //Assign our safe attributes 
      $model->attributes=$_POST['FaDepreciation']; 

      //Save the model to the database 
      if($model->save()){ 
       $this->upload($model->id); 
       $this->redirect(array('view','id'=>$model->id)); 
      }   
     } 

     $this->render('create',array(
      'model'=>$model, 
     )); 

    } 

답변

0

Q1 : 어떻게 아약스를 통해 이미지를 삭제 중 후 CMultiFileUpload 위젯을 표시하려면?

  • 은 마우스 오른쪽 위젯을 렌더링

    • 는 별도의 컨트롤러를 생성하고 당신의 위젯 (요청이 이미지의 제거 후 아약스를 통해 전송됩니다) 렌더링 :

  • 두 가지 옵션이 있습니다 페이지를 삭제하고 CSS를 통해 표시 할 수 있습니다.

    질문 2 : 이미지를 즉시 업로드 한 후 이미지를 표시하는 방법은 무엇입니까?

    • 가장 확실한 방법은 이미지와 HTML 블록을 조립 JSON으로 클라이언트 측의 응답을 보내는 것입니다.
    • 두 번째 옵션은 이미지로 페이지를 렌더링하고 고객
    +0

    @Korotovsky에 보낸, 당신이 나에게 자세한 내용을 알 수 서버에? 나는 아주 새로운 yii입니다. –

    +0

    매우 추상적 인 질문을했습니다. 내가 당신에게 조언 할 수있는 것은이 가이드를 읽고 블로그 (http://www.yiiframework.com/doc/blog/)와 모든 요리법 (http://www.yiiframework.com/doc/guide/)을 작성하십시오. –

    관련 문제