Pergunta

I'm using uploadify to upload files. I would like to throw error and stop submitting/uploading the form if the total upload size of all files are greater than 10MB.

I browsed the document of uploadify http://www.uploadify.com/documentation/ and found "fileSizeLimit" which limits file size of a single file. But I want total upload size of all files together.

Can some one suggest me how to do this?? Below is my code

<script>
$(function() {
$('#file_upload').uploadify({    
    'formData' : {
        'timestamp' : '<?php echo $timestamp;?>',
        'token'     : '<?php echo md5('unique_salt' . $timestamp);?>'
    },
    'swf'      : 'uploadify.swf',
    'uploader' : 'uploadify.php',

    'auto'     : false,        
    'buttonText' : 'Upload Document',
    'fileTypeExts' : '<?php echo $allowedFileExts; ?>',
    'fileSizeLimit' : '10MB',
    'onQueueComplete' : function(event,data) {            
        document.someForm.submit();
        document.someForm.reset();
    }
});
});
</script>
<form id="someForm" name="someForm" action="test.php" method="post" enctype="multipart/form-data">
<input id="file_upload" name="file_upload" type="file" multiple="true"/>
<a href="javascript:$('#file_upload').uploadify('cancel','*');">Clear Queue</a>
<input onclick="$('#file_upload').uploadify('upload','*')" type="button" id="submitbtn" name="submitbtn" value="Submit" />
<div id="queue"></div>
</form>   
Foi útil?

Solução

Atlast, I found my own solution for my issue. Thanks all for your support. My code is below. might be helpful for someone.

<script type="text/javascript">   
<?php $timestamp = time();?>
var totalfilesize = 0;
$(function() {
var queueSize = 0;
$('#file_upload').uploadify({    
    'formData' : {
        'timestamp' : '<?php echo $timestamp;?>',
        'token'     : '<?php echo md5('unique_salt' . $timestamp);?>'
    },
    'swf'      : 'uploadify.swf',
    'uploader' : 'uploadify.php',

    'auto'     : false,        
    'buttonText' : 'Upload Document',
    'fileTypeExts' : '<?php echo $allowedFileExts; ?>',
    'fileSizeLimit' : '10MB',        
    'onSelectError' : function() {
        alert('The file ' + file.name + ' returned an error and was not added to the queue.');
    },
    'onSelect' : function(file){
        queueSize++;
        $('#queuesize').val(queueSize);
        totalfilesize = totalfilesize+file.size;
        chkfilesize(totalfilesize);
    },
    'onCancel' : function(file) { 
        queueSize--;
        $('#queuesize').val(queueSize);
        totalfilesize = totalfilesize-file.size;
        chkfilesize(totalfilesize);             
    },
    'onUploadStart' : function(file) {            
    },
    'onUploadSuccess' : function(file, data, response) {
        //$("#response").html(data);            
        $('#uploadedfiles').val($('#uploadedfiles').val() + data);            
    },
    'onQueueComplete' : function(event,data) {            
        document.someForm.submit();
        $('#queuesize').val('0');
        $('#uploadedfiles').val('');            
        document.someForm.reset();

    }
});
 });

function chkfilesize(totalfilesize){
if(totalfilesize > 10485760){        
    $('#fileerror').html('File Size exceeds 10MB');
    $('#fileerror').show();
}else{        
    $('#fileerror').html('');
    $('#fileerror').hide();
}

}
function submitForm() { 
var validatefileresp = validatefile();    
if(validatefileresp === true){
    $('#file_upload').uploadify('upload','*');
}
}

function validatefile(){  
if ($("#queuesize").val() == 0) {
    document.someForm.submit();
    $('#uploadedfiles').val('');
    $('#queuesize').val('0');        
    document.someForm.reset();        
}else{
    if(totalfilesize > 10485760){
        $('#fileerror').html('File Size exceeds 10MB');
        return false;
    }else{
        return true;
    }

}

}
</script>
<form id="someForm" name="someForm" action="test.php" method="post" enctype="multipart/form-data">
First name: <input name="fname" type="text" /><br>
Last name: <input name="lname" type="text" /><br><br><br>
Upload only doc,pdf,docx,xls,xlsx,txt,rtf format files<br>
<input id="file_upload" name="file_upload" type="file" multiple="true"/>
<input type="hidden" name="uploadedfiles" id="uploadedfiles" value=""  />
<input type="hidden" name="queuesize" id="queuesize" value="0" />
<br><br>
<a href="javascript:$('#file_upload').uploadify('cancel','*');">Clear Queue</a>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input onclick="javascript:submitForm();" type="button" id="submitbtn" name="submitbtn" value="Submit" />
<div id="fileerror"></div>
<div id="queue"></div>

<div id="response"></div>
</form>   

Outras dicas

Try sizeLimit

Look at this line **

else if (d.type ==="File Size")
     alert(c.name+' '+d.type+' Limit: '+Math.round(d.sizeLimit/1024)+'KB');** 

in:

 onError: function (a, b, c, d) {
         if (d.status == 404)
            alert('Could not find upload script. Use a path relative to: '+'<?= getcwd() ?>');
         else if (d.type === "HTTP")
            alert('error aaa'+d.type+": "+d.status);
         else if (d.type ==="File Size")
            alert(c.name+' '+d.type+' Limit: '+Math.round(d.sizeLimit/1024)+'KB');
         else
            alert('error '+d.type+": "+d.text);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top