This is a migrated thread and some comments may be shown as answers.

Validation on File Size

2 Answers 74 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
Jayzle
Top achievements
Rank 1
Jayzle asked on 24 Aug 2015, 09:24 AM

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 Answers, 1 is accepted

Sort by
0
Nencho
Telerik team
answered on 27 Aug 2015, 08:38 AM
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
0
Jayzle
Top achievements
Rank 1
answered on 27 Aug 2015, 09:29 AM

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

Tags
AsyncUpload
Asked by
Jayzle
Top achievements
Rank 1
Answers by
Nencho
Telerik team
Jayzle
Top achievements
Rank 1
Share this question
or