How to validate file size before upload at client side ( Javascript ) ?

  1. #1

    How to validate file size before upload at client side ( Javascript ) ?

    Hi all,

    I'm using control "FileUploadField" to upload file to the server. The file to be upload is required to not exceed maximum size.

    Therefore, I wonder how can I check the size of the file before uploading , by Javascript ?

    Thanks for help.

    My code :

    + FileUploadField :
     <ext:FileUploadField ID="FileUploadFieldAttachment" runat="server" Width="212" />
    + Button Action :
       <ext:Button ID="ButtonUpload" runat="server" Text="Upload">
                                            <Listeners>
                                                <Click Fn="ValidateFileSize"></Click>
                                            </Listeners>
                                            <DirectEvents>
                                                <Click OnEvent="InsertNewAttachment">
                                                </Click>
                                            </DirectEvents>
                                        </ext:Button>
    I have used this function, but it does not work :
     function ValidateFileSize() {
                var node = document.getElementById('ChildPage_FileUploadFieldAttachment-file');
                alert(node);
                if (node != null) {
                    alert(node.files);
                    if (node.files != null) {
                        var check = node.files[0];
                        alert(check);
                        if (check != null) {
                            var checkSize = check.fileSize;
                            alert(checkSize);
                            if (checkSize / 1024 > 10240) {
                                Ext.Msg.alert('Confirm', "Your file is too big !");
                                return false;
                            }
                        }
                    }
                }
                return true;
            }
    Last edited by fac; Jan 14, 2012 at 5:36 AM.
  2. #2
    Hi,

    The browser security policy to don't allow JavaScript full free to work with local files, therefore I'm not sure there is a good cross-browser solution.

    I would look it on the internet. For example:
    http://www.kavoir.com/2009/01/check-...uploading.html
  3. #3
    Thanks for your help. I configed web.config with MaxRequestLength attribute in HttpRuntime tag.

Similar Threads

  1. [CLOSED] multiple file upload and file size at client side
    By mirwais in forum 1.x Legacy Premium Help
    Replies: 24
    Last Post: Dec 15, 2014, 5:44 AM
  2. [CLOSED] How to identify upload file size before uploading in IE?
    By rnachman in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Jun 29, 2012, 5:54 PM
  3. [CLOSED] Validate file size using FileUpload in client side
    By Pablo_Azevedo in forum 1.x Legacy Premium Help
    Replies: 3
    Last Post: Oct 28, 2011, 4:12 PM
  4. Replies: 1
    Last Post: Jun 23, 2011, 9:37 AM
  5. Maximum file size for upload
    By CoolNoob in forum 1.x Help
    Replies: 3
    Last Post: Jan 07, 2010, 3:57 PM

Tags for this Thread

Posting Permissions