0

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.

Akilan
  • 1,707
  • 1
  • 16
  • 30

4 Answers4

1

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

Abudayah
  • 3,816
  • 7
  • 40
  • 60
  • I am using CMultiFileUpload, here how can i use CFileValidator. And I want to use on file select itself. Can u please explain little more? – Akilan Jun 20 '13 at 05:35
  • @Akilan First you have to copy this in your model and validate via ajax maybe you have to do custom action. check this: http://learnyii.blogspot.com/2010/12/yii.html – Abudayah Jun 20 '13 at 13:33
1

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;

                }',
chmoth
  • 23
  • 1
  • 6
  • Could you elaborate a bit more on how that will work? – The Guy with The Hat Jan 21 '14 at 12:48
  • you mean the code i gave? if yes, then the code will find the the currently selected files size and check wether its more than 800kb. alert an error and clicks the remove button on the widget.. the file gets removed, so you can upload another one – chmoth Feb 05 '14 at 16:19
  • If, for ex., 3 files have been uploaded and only 3rd file is too big, ALL files will be removed by `$(".MultiFile-remove").click();`. – Boolean_Type Feb 05 '19 at 12:52
0

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;

                    }',                                                       
             ),




        ));   
Elango Mani
  • 354
  • 4
  • 21
0

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>
Boolean_Type
  • 1,146
  • 3
  • 13
  • 40
  • me too!, me too!, me too! - not an answer. please restrain from answering old questions if not of real value. – hakre May 11 '14 at 10:09
  • If, for ex., 3 files have been uploaded and only 3rd file is too big, ALL files will be removed by `$("#photos").reset();`. – Boolean_Type Feb 05 '19 at 14:57