Validate File size before upload

后端 未结 3 1683
野性不改
野性不改 2021-01-06 19:14

I need to validate the file which is to be uploaded to the server. The validation must be done before uploading it. i.e., validation completed at client side. This task shou

3条回答
  •  广开言路
    2021-01-06 19:34

    You can achieve by using jquery:

    #
    
    Attachment (8 MB only)
    
    
    
    #
    jQuery(document).ready(function () {
    
    
    jQuery('#Attachment').bind('change', function () {
                                //fileUpload = 0;
                                var iSize = (this.files[0].size / 1024);
                                if (iSize / 1024 > 1) {
                                    if (((iSize / 1024) / 1024) > 1) {
                                        fileUpload = 0;
                                    } else {
                                        iSize = (Math.round((iSize / 1024) * 100) / 100);
                                        if (iSize <= 8) {
                                            fileUpload = 1;
                                        } else {
                                            fileUpload = 0;
                                        }
                                    }
                                } else {
                                    fileUpload = 1;
                                }
                                if (fileUpload == 0) {
                                   // alert("Your attachment exceeded 8MB.");
                                    jQuery('#attached').html('Your attachment exceeded 8MB.');
                                    jQuery('#Attachment').val('');
                                }
    
                            });
    
                        });
    

提交回复
热议问题