Can't get upload file working

Hi everybody !

I followed wiki and searched all topics about file uploading but can’t get it working ! :frowning:

My model :




class Ticket extends CActiveRecord

{

	public $file;

	...

	public function rules()

	{

		return array(

			...

			array('file', 'file', 'types' => 'png'),

			...

		);

	}


	...


	public function beforeSave(){

		$this->file = CUploadedFile::getInstance($model, 'file');

		$this->file_name=$this->file->name;

		$this->file_type=$this->file->type;

		$this->file_size=$this->file->size;

		$this->file_content=file_get_contents($this->file->tempName);

		

		return parent::beforeSave();

	}

}



My controller :




class TicketController extends Controller{


	...


	public function actionAdd(){

		

		$model = new Ticket();

		

		if(isset($_POST['Ticket'])){

			$model->attributes = $_POST['Ticket'];


			if($model->validate()){

				$model->save(false);

				Yii::app()->end();

			}

		}

		

		$this->renderPartial('_form', array(

			'model' => $model,

		));

	}


	...


}



My view :




<div class="form">

    

	<?php

		echo CHtml::form('', 'post',  array('id' => 'ticket-form', 'enableAjaxValidaion' => false, 'enctype'=>'multipart/form-data'));

	?>


	...


	<div class="row">

		<?php echo CHtml::activeLabelEx($model,'file'); ?>

		<?php echo CHtml::activeFileField($model, 'file'); ?>

		<?php echo CHtml::error($model,'file'); ?>

	</div>


	<div class="buttons">

            <?php

                echo IDHtml::ajaxCancelButton('Annuler');

                echo IDHtml::ajaxSubmitButton($model->isNewRecord ? 'Créer' : 'Enregistrer');

            ?>

        </div>

</div>



Example :

When I go to my form page I have :

1939

upload-before-validate.png

I submit my form and my field is now empty with an error message :

1940

upload-after-validate.png

Do you have any idea about that ?

Thanks !

Upload to DB

Upload using a model

try refer back to this 2 link… Hope this can help u.

Thanks but as I said, I already refered to those links and others on the wiki and forum.


public function beforeSave(){

                $this->file = CUploadedFile::getInstance($model, 'file');

                $this->file_name=$this->file->name;

                $this->file_type=$this->file->type;

                $this->file_size=$this->file->size;

                $this->file_content=file_get_contents($this->file->tempName);

                

                return parent::beforeSave();

        }



$this->file_name=$this->file->name;

Your code seem very weird. i think you use back the function in wiki and check whether can or not.

Yes I use code from wiki which says :




$this->file_name=$file->name;

$this->file_type=$file->type;

$this->file_size=$file->size;

$this->file_content=file_get_contents($file->tempName);



And all those fields are in my database, no problem with that.

My problem is from




$this->file = CUploadedFile::getInstance($this, 'file');



Which returns NULL.

The question is why ?

change to $file = CUploadedFile::getInstance($this, ‘file’);

I already tried but I re-try with this code :




$file = CUploadedFile::getInstance($this, 'file');

$this->file_name=$file->name;

$this->file_type=$file->type;

$this->file_size=$file->size;

$this->file_content=file_get_contents($file->tempName);



Always the same problem. But thanks for helping ! :slight_smile:

array(‘file’, ‘safe’)

array(‘file’, ‘file’,

            'types' =&gt; 'jpg, gif, png',


            'allowEmpty' =&gt; true)

With ‘allowEmpty’ => true :

I don’t have any error but nothing is saved in my database about the file.


if($model->validate()){

                                $model->save(false);

                                Yii::app()->end();

                        }



why you set the $model->save(false);

change to $model->save() instead $model->save(false);

It doesn’t change anything.

I do $model->save(false); because I do before $model->validate() and I don’t need to validate again my model.

Ok, so I solved a part of my problem.

All what I did before is correct but I can get it working when doring $this->render(…) and not $this->renderPartial(…); in my Controller.

Anyone knows Why I can’t have a mutlipart-form in a renderPartial ??

If I don’t have choice, I’ll do it with render but it’s not what I want exactly !

No one already tried to do a file upload in a renderPartial ?

Thanks !

Hi,

FlorianBezagu - i have the same problem. Searching the web for hours without success.

If you take a look at the wiki you can see, that there are using render, not renderPartial.

http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/

But in fact: the following view does not work with render, but it works with renderPartial (same as yours):




<?php $formUpload = $this->beginWidget('CActiveForm', array(

    'id'=>'formUpload',

	'action' =>CController::createUrl('Qr/create',array('s'=>'createUpload')),

	'htmlOptions' => array('enctype' => 'multipart/form-data'),

)); ?>


<div >

    <?PHP

          echo $formUpload->fileField($modelUpload,'Z');

          echo CHTML::ajaxSubmitButton('pxl',

          				CController::createUrl('Qr/create',array('s'=>'createUpload')),

 					array('success'=> ' [...]')

          				);

    ?>

</div>


<?php $this->endWidget(); /*Ende $formUpload*/ ?>



But: if i split the view in order to renderPartial the Form and only render the rest: it does not work?!

So why we both have this Problem?

The common thing I see is, that we both use ajaxSubmitButton. Maybe this is the reason why?

Could this be a bug?

Have you tried to open an issue for that?

Rall0r

First of all you should know that ajax upload does not work in all browsers, so even if you get this working you need to check how this works in different browsers…

ajaxSubmitButton does generate HTML and jQuery code… the problem you have (as I see it) is the jQuery code that gets generated by ajaxSubmitButton… when the code is working for you it means that the jQuery code is on the generated page, when it’s not working that code most probably is not there…

so try to check the page source to check if you get there the needed code…

In general Yii methods are very easy to use and very helpful for easy situations… but for any more complicated situation it’s much better to create manual code… so if you study the code generated by ajaxSubmitButton I believe you can create a custom solution that would use the standard submitButton and your custom jQuery code

Hi mdomba,

Oh, thats new for me. I didn’t thought, that AjaxUpload is an special case.

But if CActiveForm and AjaxSubmitButton supports Ajax-submtis, wouldn’t it be nice (and wouldn’t it be consequent) that (CActive)Fileupload supports that feature by implementing one oft those solutions from here (for example) http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery ?