Important notes
The selected files must be transferred to the server in order to be validated for size or mime-type. The file extensions can be validated on the client, before the upload.
To enable uploading of files, larger than 4MB, you must set the maxRequestLength attribute of the httpRuntime section in your web.config file.
Using the ValidatingFile event
RadUpload provides the ValidatingFile server-side event, which can be used to define custom validation logic and to override the internal validation if needed.
When a file is validated, the IsValid property of the event arguments must be set according the result of the validation.
Use the SkipInternalValidation property of event arguments to disable the internal validation for the specific file.
The valid files can be later accessed using the UploadedFiles property. The invalid files can be accessed using the InvalidFiles property if needed.
The example below demonstrates how to implement custom file size validation only for zip files. The rest of the uploaded files are validated against the declared maximum file size. The valid files are automatically saved in the "~/MyFiles" folder.
ASPX
|
<rad:radupload id="RadUpload1" runat="server" maxfilesize="1000000" targetfolder="~/MyFiles" onvalidatingfile="RadUpload1_ValidatingFile"></rad:radupload> <asp:button Runat="server" ID="Button1" Text="Submit"></asp:button>
|
VB.NET
|
Imports Telerik.WebControls ... Private Sub RadUpload1_ValidatingFile(ByVal sender As Object, ByVal e As WebControls.ValidateFileEventArgs) If e.UploadedFile.GetExtension.ToLower = ".zip" Then Dim maxZipFileSize As Integer = 2000000 If e.UploadedFile.ContentLength > maxZipFileSize Then e.IsValid = False End If e.SkipInternalValidation = True End If End Sub
|
C#
|
using Telerik.WebControls; ... private void RadUpload1_ValidatingFile(object sender, ValidateFileEventArgs e) { //we will 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 the maximum file size mark the file as invalid if (e.UploadedFile.ContentLength > maxZipFileSize) { e.IsValid = false; } //The zip files must not be validated for file size from the internal validator e.SkipInternalValidation = true } }
|