Search code examples
javascriptphpjsonyiijquery-file-upload

Add a file description when uploading file Yii 1


Hello I'm using EAjaxUpload extension to upload files and its working perfectly and files are uploaded I want to add a description to each file. I used the onComplete to have the function here's my code :

$uploadfile = $this->widget('ext.EAjaxUpload.EAjaxUpload',
                        array(
                            'id' => 'uploadFile',
                            'config' => array(
                                'action' => Yii::app()->createUrl('objective/upload'),
                                'allowedExtensions' => array("docx", "pdf", "pptx"),//array("jpg","jpeg","gif","exe","mov" and etc...
                                'sizeLimit' => 5 * 1024 * 1024,// maximum file size in bytes
                                //'minSizeLimit'=>10*1024*1024,// minimum file size in bytes
                                'onComplete' => "js:function(id, fileName, responseJSON){
                                console.log(responseJSON);
                                var filedescription= prompt('file description');
                                  if (filedescription != null) {

                                           document.getElementById('demo').innerHTML =
                                 filedescription;
                                    return filedescription;
                                          }
                                 }",
                                //'messages'=>array(
                                //                  'typeError'=>"{file} has invalid extension. Only {extensions} are allowed.",
                                //                  'sizeError'=>"{file} is too large, maximum file size is {sizeLimit}.",
                                //                  'minSizeError'=>"{file} is too small, minimum file size is {minSizeLimit}.",
                                //                  'emptyError'=>"{file} is empty, please select files again without it.",
                                //                  'onLeave'=>"The files are being uploaded, if you leave now the upload will be cancelled."
                                //                 ),
                                'showMessage' => "js:function(message){ alert(message); }"
                            )
                        )); 

Now I want to return var filedescription to upload action in controller. How can I do that?

Thanks,


Solution

  • 1.onComplete is called after your upload request is already processed by "objective/upload" action. So you have possibility to ask and set description as parameter BEFORE request:

    'onSubmit' => "js:function(id, fileName){
        // add filedescriton to post parameters:
        this.params.filedescription = 'some file description';
    }"
    

    In controller "objective/upload" action you can access it as $_POST['filedescription'].

    2.Other possibility is to create separate action for saving description (and additional file processing...) and call it from onComplete:

    In onComplete:

    $.post( 'saveUploadedFileDescription', { filedescription: 'some file description', fileName: fileName } );
    

    In controller:

    actionSaveUploadedFileDescription($filedescription,$filename) {
        // ....
    }