Validation on File Size

3 posts, 0 answers
  1. Jayzle
    Jayzle avatar
    28 posts
    Member since:
    Dec 2012

    Posted 24 Aug 2015 Link to this post

    Usually for validation, it involves allowed file types but just want to ask how to validate file size only? Thanks.

    I also noticed when setting the error message, first it's like checking if message is about file type then else it becomes error regarding file size as show below:

    function getErrorMessage(sender, args) {
            var fileExtention = args.get_fileName().substring(args.get_fileName().lastIndexOf('.') + 1, args.get_fileName().length);
            if (args.get_fileName().lastIndexOf('.') != -1) {//this checks if the extension is correct
                if (sender.get_allowedFileExtensions().indexOf(fileExtention) == -1) {
                    return ("This file type is not supported.");
                }
                else {
                    return ("This file exceeds the maximum allowed size of 500 KB.");
                }
            }
            else {
                return ("not correct extension.");
            }
        }

  2. Nencho
    Admin
    Nencho avatar
    1457 posts

    Posted 27 Aug 2015 Link to this post

    Hello Jayzle,

    Yes, you can validate only the file size limit. Actually, if we assume that you don't have set the file types and extensions - the event will be triggered only if the max file size is exceed. In other words, you can directly display the message for the exceeded size, if the OnClientValiaditionFailed client-side event is triggered.

    However, if you have defined the AllowedFileExtensions and MaxFileSize, you should use the demonstrated code-snippet, in order to differentiate which is the exact error thrown, as the OnClientValidationFaild suites for both errors.

    Regards,
    Nencho
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Jayzle
    Jayzle avatar
    28 posts
    Member since:
    Dec 2012

    Posted 27 Aug 2015 in reply to Nencho Link to this post

    Hi Nencho,

     Yes, that's what I did as shown below. Thank you for your response.

    function OnValidationFailed(sender, args) {
                var $row = $(args.get_row());
                var erorMessage = "File exceeds maximum allowed size of 5MB and will not be uploaded.";
                var span = createError(erorMessage);
                $row.addClass("ruError");
                $row.append(span);                                
                 
            }

Back to Top