2012-08-02 2 views
1

1 분기 : 제출 양식이 작동하지 않습니다.yii ajax xupload 양식 제출이 작동하지 않습니다.

Q2 : 제한하는 방법 업로드 파일 (예 1-5 파일 만)

상태 : 아약스 업로드와 양식을 만들 xupload

내 모델 (fadepreciation.php)

public function afterSave() { 
     $this->addImages(); 
     parent::afterSave(); 
    } 

    public function addImages() { 
     //If we have pending images 
     if(Yii::app()->user->hasState('images')) { 
      $userImages = Yii::app()->user->getState('images'); 
      //Resolve the final path for our images 
      $path = Yii::app()->getBasePath()."/../images/uploads/{$this->id}/"; 
      //Create the folder and give permissions if it doesnt exists 
      if(!is_dir($path)) { 
       mkdir($path); 
       chmod($path, 0777); 
      } 

      //Now lets create the corresponding models and move the files 
      foreach($userImages as $image) { 
       if(is_file($image["path"])) { 
        if(rename($image["path"], $path.$image["filename"])) { 
         chmod($path.$image["filename"], 0777); 
         $img = new Image(); 
         $img->size = $image["size"]; 
         $img->mime = $image["mime"]; 
         $img->name = $image["name"]; 
         $img->source = "/images/uploads/{$this->id}/".$image["filename"]; 
         $img->somemodel_id = $this->id; 
         if(!$img->save()) { 
          //Its always good to log something 
          Yii::log("Could not save Image:\n".CVarDumper::dumpAsString( 
           $img->getErrors()), CLogger::LEVEL_ERROR); 
          //this exception will rollback the transaction 
          throw new Exception('Could not save Image'); 
         } 
        } 
       } else { 
        //You can also throw an execption here to rollback the transaction 
        Yii::log($image["path"]." is not a file", CLogger::LEVEL_WARNING); 
       } 
      } 
      //Clear the user's session 
      Yii::app()->user->setState('images', null); 
     } 
    } 

내보기 (_form.php)

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'fa-depreciation-form', 
    'enableAjaxValidation'=>false, 
    'htmlOptions' => array('enctype' => 'multipart/form-data'), 
)); ?> 

    <p class="note">Fields with <span class="required">*</span> are required.</p> 

    <?php echo $form->errorSummary($model); ?> 
<!-- Other Fields... --> 
     <div class="row"> 
      <?php echo $form->labelEx($model,'photos'); ?> 
      <?php 
      $this->widget('xupload.XUpload', array(
       'url' => Yii::app()->createUrl("/fadepreciation/upload"), 
       //our XUploadForm 
       'model' => $photos, 
       //We set this for the widget to be able to target our own form 
       'htmlOptions' => array('id'=>'fa-depreciation-form'), 
       'attribute' => 'file', 
       'multiple' => true, 
       //Note that we are using a custom view for our widget 
       //Thats becase the default widget includes the 'form' 
       //which we don't want here 
       //'formView' => 'application.views.faDepreciation._form', 
       )  
      ); 
      ?> 
     </div> 
    <div class="row buttons"> 
     <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?> 
    </div> 

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

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

내 컨트롤러 (fadepreciation.php)

public function actionCreate() 
    { 
     $model=new FaDepreciation; 
     Yii::import("xupload.models.XUploadForm"); 
     $photos = new XUploadForm; 
     // Uncomment the following line if AJAX validation is needed 
     // $this->performAjaxValidation($model); 

     if(isset($_POST['FaDepreciation'])) 
     { 
      //Assign our safe attributes 
      $model->attributes=$_POST['FaDepreciation']; 
      //Start a transaction in case something goes wrong 
      $transaction = Yii::app()->db->beginTransaction(); 
      try { 
       //Save the model to the database 
       if($model->save()){ 
        $transaction->commit(); 
        $this->redirect(array('view','id'=>$model->id)); 
       } 
      } catch(Exception $e) { 
       $transaction->rollback(); 
       Yii::app()->handleException($e); 
      } 
      if($model->save()) 
       $this->redirect(array('view','id'=>$model->id)); 
     } 

     Yii::import("xupload.models.XUploadForm"); 
     $photos = new XUploadForm; 
     $this->render('create',array(
      'model'=>$model, 
      'photos'=>$photos, 
     )); 

    } 
public function actionUpload() // From xupload nothing change 

답변

0

제출 양식의 문제점은 무엇입니까?

예 파일 제한을 수행 할 수 있습니다. 이를 따라야합니다. http://www.yiiframework.com/wiki/348/xupload-workflow/

+0

나는 xupload의 위키를 따라 갔다. 파일 업로드가 정상적으로 작동합니다. 하지만 제출 버튼을 클릭하면 아무런 조치도없고 아무런 오류도 표시되지 않습니다. 파일 제한은 'multiple'=> true, 'multiple'=> false, right라고 생각하십니까? 하지만 업로드하는 데 1-5 개의 파일을 제한하고 싶습니다. 어디에서 설정할 수 있습니까? –

1

사용자 정의 양식을 작성하기 만하면됩니다. xupload _form에서 내용을 복사하여 붙여 넣기 시작 양식을 제거하십시오. 위젯에 추가 'formView'사용자 정의 양식에서 참조하십시오.

0

Q1 : 양식 제출이 작동하지 않습니다. XUpload 위젯이 자체 양식 태그를 생성하기 때문입니다. 이렇게 생성 된 HTML은 또 다른 형태의 양식 embebed을 가지고, 당신은 xupload workflow wiki

Q2에 설명 된대로, 어떤 양식 태그가없는보기를 가리 키도록 위젯의 formView 옵션을 사용해야합니다 : 당신은 위젯에 maxNumberOfFiles 옵션을 사용해야합니다 그것은 모두가 다음과 같아야합니다 설정

:

<?php 
      $this->widget('xupload.XUpload', array(
       'url' => Yii::app()->createUrl("/fadepreciation/upload"), 
       //our XUploadForm 
       'model' => $photos, 
       //We set this for the widget to be able to target our own form 
       'htmlOptions' => array('id'=>'fa-depreciation-form'), 
       'attribute' => 'file', 
       'multiple' => true, 
       //Note that we are using a custom view for our widget 
       //Thats becase the default widget includes the 'form' 
       //which we don't want here 
       'formView' => 'application.views.faDepreciation._form', 
       'options' => array('maxNumberOfFiles' => 5) 
       )  
      ); 
      ?> 
0

그냥 다음과 같이 'showForm'매개 변수를 사용

<?php 
$this->widget('xupload.XUpload', array(
    ... 
    'showForm' => false, 
    ... 
)); 
?> 

아마도이 옵션은 xupload의 다음 버전에 추가되었습니다.

0

나는 오래 된 게시물이지만이 대답은 누군가가이 문제를 해결하는 데 도움이 될 것입니다.

나는 그것이 /xupload/views/form.php 파일의 마지막 줄 (디폴트 설정으로)에 의해 발생한다는 것을 알았다. if 문이 어떤 식 으로든 반대 작업을하는 것처럼 보입니다 ... 잘못된 값으로 코드를 렌더링한다는 마이닝에서.

<?php 
echo $this->showForm; 
if($this->showForm) echo CHtml::endForm(); 
echo $this->showForm; 
?> 

반환 : 예를 들어 weirg output

아마 ...하지 않는 내가 뭔가를보고 싶어하지만 이상한 보인다?

관련 문제