Custom Validation
RadUpload has been replaced by RadAsyncUpload, Telerik’s next-generation ASP.NET upload component. If you are considering Telerik’s Upload control for new development, check out the documentation of RadAsyncUpload or the control’s product page. If you are already using RadUpload in your projects, you may be interested in reading how easy the transition to RadAsyncUpload is and how you can benefit from it in this blog post. The official support for RadUpload has been discontinued in June 2013 (Q2’13), although it is still be available in the suite. We deeply believe that RadAsyncUpload can better serve your upload needs and we kindly ask you to transition to it to make sure you take advantage of its support and the new features we constantly add to it.
In addition to the integrated validation, RadUpload lets you perform your own custom validation using the server-side ValidatingFile event. By using this event, you can use your own custom validation logic and override the integrated validation if needed.
To validate a file in the ValidatingFile event handler, set the IsValid property of the event arguments to true or false, in order to indicate whether the file is valid. Then set the SkipInternalValidation property of the event arguments to true to bypass the default validation on any files you have validated using the event handler.
As with integrated validation, valid files can be accessed using the UploadedFiles property and invalid files using the InvalidFiles property.
The following example illustrates using custom validation to bypass the integrated size validation for zip files. The rest of the uploaded files are validated against the declared maximum file size:
<telerik:radupload id="RadUpload1" runat="server" maxfilesize="1000000" targetfolder="~/MyFiles"
onvalidatingfile="RadUpload1_ValidatingFile"></telerik:radupload>
<asp:Button runat="server" ID="Button1" Text="Submit" />
using Telerik.Web.UI;
using Telerik.Web.UI.Upload;...
private void RadUpload1_ValidatingFile( object sender, ValidateFileEventArgs e)
{
// check only the zip files
if (e.UploadedFile.GetExtension().ToLower() == ".zip")
{
//define the maximum file size to be 2000000 bytes
int maxZipFileSize = 2000000;
//if the zip file size exceeds 2000000 bytes mark it as invalid
if (e.UploadedFile.ContentLength > maxZipFileSize)
{
e.IsValid = false;
}
// zip files must not be validated for file size by the internal validator
e.SkipInternalValidation = true;
}
}
For an example of custom validation that checks the content of the uploaded file, see ValidatingFile.