Skip Navigation LinksHome / Community & Support / Code Library / ASP.NET and ASP.NET AJAX > Upload > Client-Side file size validation in RadAsyncUpload

Not answered Client-Side file size validation in RadAsyncUpload

Feed from this thread
  • Matthew avatar

    Posted on Jan 4, 2011 (permalink)

    Requirements

    RadControls Q3 2010 SP1

    .NET 4.0

    Visual Studio 2010

    programming c#, Javascript

    browser support

    all browsers supported by RadControls


    PROJECT DESCRIPTION
    Aim: To gain some kind of client side validation to restrict users from adding more than a pre-determined limit of files based upon total size of all uploaded files, not just each file or number of files...

    Add the following to an ASP.NET page.

    <table>
                    <tr>
                        <td colspan="2">You may attach up to 20mb in files to the Email.</td>
                    </tr>
                         <tr>
                            <td>Select File(s):</td>
                            <td>
                               <telerik:RadAsyncUpload ID="RadAsyncUpload1"
                               OnClientFileSelected="OnFileSelected"
                               OnClientFileUploadFailed="OnFileUploadFailed"
                               OnClientFilesUploaded="OnFilesUploaded"
                               OnClientProgressUpdating="OnProgressUpdating"
                               OnClientFileUploaded="OnFileUploaded"
                               OnClientFileUploadRemoved="OnFileUploadRemoved"
                               runat="server">
                               </telerik:RadAsyncUpload>
                             </td>
                        </tr>
                    </table>


    Then I linked the following external javascript file (You could also add in the head section of your page in a <script> block.

    var uploadsInProgress = 0;
    var MAX_TOTAL_BYTES = 20971520;
    var filesSize = new Array();
    var OVERSIZE_MESSAGE = "You are only allowed to add up to 20mb of files total";
    var isDuplicateFile = false;
     
    function OnFileSelected(sender, args) {
     
        for (var fileindex in sender._uploadedFiles) {
            if (sender._uploadedFiles[fileindex].fileInfo.FileName == args.get_fileName()) {
                isDuplicateFile = true;
            }
        }
        if (!uploadsInProgress) {
            $("input[id$=btnSave]").attr("disabled", "disabled");
            
        }
        uploadsInProgress++;
    }
     
    function OnFilesUploaded(sender, args) {
        if (sender._uploadedFiles.length == 0) {
            filesSize = new Array();
            uploadsInProgress = 0;
            $("input[id$=btnSave]").removeAttr("disabled");
        }
        if (uploadsInProgress > 0) {
            DecrementUploadsInProgress();
        }
         
    }
     
    function OnProgressUpdating(sender, args) {
         
            filesSize[args.get_data().fileName] = args.get_data().fileSize;
         
    }
     
    function OnFileUploadFailed(sender, args) {
        DecrementUploadsInProgress();
    }
     
    function OnFileUploaded(sender, args) {
        DecrementUploadsInProgress();
        var totalBytes = 0;
        var numberOfFiles = sender._uploadedFiles.length;
         
            if (isDuplicateFile) {
                sender.deleteFileInputAt(numberOfFiles - 1);
                isDuplicateFile = false;
                sender.updateClientState();
                alert("You can't add the same file twice");
                return;
            }
         
        for (var index in filesSize) {
            totalBytes += filesSize[index];
        }
        if (totalBytes > MAX_TOTAL_BYTES) {
            sender.deleteFileInputAt(numberOfFiles - 1);
            sender.updateClientState();
            alert(OVERSIZE_MESSAGE);
        }
    }
     
    function OnFileUploadRemoved(sender, args) {
        if (args.get_fileName() != null) {
            if (!isDuplicateFile) {
                delete filesSize[args.get_fileName()];
            }
        }
    }
     
    function DecrementUploadsInProgress() {
        uploadsInProgress--;
        if (!uploadsInProgress) {
            $("input[id$=btnSave]").removeAttr("disabled");
             
        }
    }


    Unfortunately the file seems to need to be uploaded for this to work. I tried to get it to fire the cancelUpload() in the OnFileSelected, but it is not a valid method... Any thoughts, suggestions or improvements are more than welcome.

    P.S. I have only tested this with single file selection as it is what I needed for my project. I have no idea if all the same logic will work for multi-selection.

    Reply

  • Genady Sergeev Genady Sergeev admin's avatar

    Posted on Jan 7, 2011 (permalink)

    Hello Matthew,

    Thank you for providing the code library. We have updated your telerik points as a token of gratitude for your involvement.

    Regards,
    Genady Sergeev
    the Telerik team
    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

    Reply

  • Bing Yang avatar

    Posted on Jun 17, 2011 (permalink)

    Hi Sir:/Madam:

    I have the same question: the file seems to need to be uploaded for this to work.

    Is there a way to get file size before uploading the file.

    Thanks
    Bing Yang

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Code Library / ASP.NET and ASP.NET AJAX > Upload > Client-Side file size validation in RadAsyncUpload