限制uploadify中所有文件的总上传大小

时间:2013-12-16 07:20:13

标签: jquery uploadify

我正在使用uploadify上传文件。如果所有文件的总上传大小超过10MB,我想抛出错误并停止提交/上传表单。

我浏览了uploadify http://www.uploadify.com/documentation/的文档,发现“fileSizeLimit”限制了单个文件的文件大小。但我希望所有文件的总上传大小。

有人可以建议我怎么做?以下是我的代码

<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>   

2 个答案:

答案 0 :(得分:0)

Atlast,我为我的问题找到了自己的解决方案。谢谢大家的支持。我的代码如下。可能对某人有帮助。

<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>   

答案 1 :(得分:0)

尝试sizeLimit

看看这一行**

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

在:

 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);