Akilan
Akilan

Reputation: 1717

yii CMultiFileUpload max file size validation

I am using CMultifileUpload for the file upload and trying to set the Maximum upload size for the file. As I searched and didn't get any in-built parameters to set the Max file size before upload.

Here my upload file code,

$filetype="avi|flv|mp4|mpeg|mov|3gp|mkv|vob|divx|mpg|wmv|wlmp";
            $this->widget('CMultiFileUpload', array(
                'model'=>$model,
                'name' => 'videofile', 
                'max'=>1,
                'accept' => $filetype,  
                'duplicate' => 'Duplicate file!', 
                'denied' => 'Invalid file type', 
                'htmlOptions'=>array('style'=>'opacity: 0;  height: 136px; width: 200px;cursor: pointer;'),
                'options'=>array(
                    'onFileSelect'=>'function(e, v, m){
                        var size=$("#videofile")[0].files[0].size;
                        alert(size);
                        if(size <=25*1024*1024){
                            $(".black_overlay").show();
                            $("#video-form").submit();
                        }else{
                            alert("File Size Exceeded");
                            $("#video-form").reset();
                            return false;
                        }
                    }',

                 ),
            ));

What i am getting is, the if condition success case is working fine, but for the failure case, the form is not reseting.

And what actually I am trying is, want to validate the file size before submitting.

Help me. Thanks in advance.

Upvotes: 0

Views: 3845

Answers (4)

user3611804
user3611804

Reputation: 1

Works for me:

<div id="multFileUpload">
<?php
$this->widget('CMultiFileUpload', array(
    'model'=>$model,
    'attribute'=>'updatePhoto',
    'accept'=>'jpg|jpeg|gif|png',
    'name'=>'photos',
    'remove'=>'remove',
    'options'=>array(
        'onFileSelect'=>'function(e ,v ,m){
            var fileSize = e.files[0].size;
            if(fileSize>1024*1024){ //1MB
                alert("Maximum file size 1MB only");
                $("#photos").reset();
                return false;
            }
        }',
    ),
    'denied'=>'File is not allowed',
    'max'=>10, //max 10 files
));
?>
</div>

Upvotes: 0

chmoth
chmoth

Reputation: 23

You can try this. this will work

'afterFileSelect'=>'function(e ,v ,m){
            var fileSize = e.files[0].size;
                 if(fileSize>800*1024){ <--800KB limit
                    alert("Exceeds file upload limit 800KB");
                    $(".MultiFile-remove").click(); <--cliks the remove button if size exceeds
                  }                      
                  return true;

                }',

Upvotes: 1

Elango Mani
Elango Mani

Reputation: 364

Cmultifileupload File Size Validation Works For Me.

`$this->widget('CMultiFileUpload', array(
            'model'=>$model,
            'name' => 'audiofile', 
            'max'=>1,
            'accept' => $filetype,  
            'duplicate' => 'Duplicate file!', 
            'denied' => 'Invalid file type', 

            'htmlOptions'=>array('style'=>'opacity: 0;  height: 80px; width: 118px;cursor: pointer;','size'=>25),
            'options'=>array(

                'afterFileSelect'=>'function(e ,v ,m){
                var fileSize = e.files[0].size;



                     if(fileSize>125*1024*1024){ 
                        alert("Exceeds file upload limit(500). Uploaded 200 MB not allowed!");
                      }
                      else
                      {
                        $(".black_overlay").show();
                        $("#audio-form").submit();
                      }

                      $("#audio-form").reset();
                      return false;

                    }',                                                       
             ),




        ));   

Upvotes: 0

Abudayah
Abudayah

Reputation: 3875

try this in your rules:

array('yourfile','file', 'types'=>'jpg, gif, png, jpeg', 'maxSize'=>1024 * 1024 * 50, 'tooLarge'=>'File has to be smaller than 50MB'),

more details:

http://www.yiiframework.com/doc/api/1.1/CFileValidator#maxSize-detail

or configure your php.ini file:

http://www.php.net/manual/en/ini.core.php#ini.upload-max-filesize

Upvotes: 1

Related Questions