RadControls for ASP.NET AJAX RadUpload can automatically validate the size, file extension and
mime-type of uploaded files against your preferences.
Caution |
|---|
The selected files must be transferred to the server in order to be validated for
size or mime-type. File
extensions can be validated on the client
using ASP.NET validators before the upload.
|
To enable integrated validation, you need only set the following RadUpload
validation properties:
Size
Set the MaxFileSize property to enable file size validation. The value of
this property specifies the maximum number of bytes allowed for each uploaded file.
AllowedFileExtensions
The AllowedFileExtensions property lists the valid file extensions for uploaded
files. When the AllowedFileExtensions property is set, RadUpload
automatically validates the extensions of selected files, moving any files with disallowed extensions to the
InvalidFiles collection.
Note |
|---|
You must include the leading dot (".") in the file extension. |
Click on the ellipsis button next to the AllowedFileExtensions property in the
properties pane, and enter each extension on a separate line in the string collection editor.
In the ASP.NET source, list the extensions in a comma-delimited list:
CopyASPX
<telerik:radupload id="RadUpload1" runat="server" allowedfileextensions=".zip,.jpg,.jpeg" />
In the code-behind, assign the value of the AllowedFileExtensions property to a string array:
CopyC#
RadUpload1.AllowedFileExtensions = new string[] {".zip", ".jpg", ".jpeg"};
CopyVB.NET
RadUpload1.AllowedFileExtensions = New String() {".zip", ".jpg", ".jpeg"}
AllowedMimeTypes
The AllowedMimeTypes property lists the valid
MIME types for uploaded files. When
the AllowedMimeTypes property is set, RadUpload
automatically validates the MIME types of selected files, moving any files with disallowed extensions
to the InvalidFiles collection.
Caution |
|---|
There are several file extensions that have multiple MIME types associated with them. Different
browsers send different MIME types for the same file. For these types of files, you need to set all associated
MIME types for the file extension you want to allow.
|
You can set multiple mime-types the same way as setting multiple file extensions.
Validated files
Valid files can be accessed using the UploadedFiles property. Files that fail
validation can be accessed using the InvalidFiles property.
After the validation, if you have specified a
target folder, the valid files are automatically saved to the target folder.
The following example shows a RadUpload control configured to receive only ZIP files
with size less than 1000000 bytes. It is configured to allow all mime-types associated with the .zip file extension.
Valid files are automatically saved to the "~/My files" folder.
CopyASPX
<telerik:radupload id="RadUpload1" runat="server" allowedmimetypes="application/x-compressed,application/x-zip-compressed,application/zip,multipart/x-zip"
maxfilesize="1000000" allowedfileextensions=".zip" targetfolder="~/My Files" />
<asp:Button runat="server" ID="Button1" Text="Submit" OnClick="Button1_Click" />
Processing Invalid files
The following example shows how to process invalid files. It provides feedback to the user about which
files were rejected, and why:
CopyC#
using Telerik.Web.UI;
...
protected void Button1_Click(object sender, EventArgs e)
{
if (RadUpload1.InvalidFiles.Count > 0)
{
Response.Write("Some of your files could not be validated<br/>");
Response.Write( "Allowed MIME types: " + string.Join(",", RadUpload1.AllowedMimeTypes) + "<br/>");
Response.Write( "Allowed file extensions: " + string.Join(",", RadUpload1.AllowedFileExtensions) + "<br/>" );
Response.Write( "Maximum file size: " + RadUpload1.MaxFileSize.ToString() + " bytes<br/><br/>");
foreach (UploadedFile f in RadUpload1.InvalidFiles)
{
Response.Write( "File name: " + f.GetName() + "<br />");
Response.Write( "File extension: " + f.GetExtension() + "<br />");
Response.Write( "Mime-type: " + f.ContentType + "<br />");
Response.Write( "File size: " + f.ContentLength.ToString() + " bytes<br />");
}
}
}
CopyVB.NET
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
If RadUpload1.InvalidFiles.Count > 0 Then
Response.Write("Some of your files could not be validated<br/>")
Response.Write("Allowed MIME types: " + String.Join(",", RadUpload1.AllowedMimeTypes) + "<br/>")
Response.Write("Allowed file extensions: " + String.Join(",", RadUpload1.AllowedFileExtensions) + "<br/>")
Response.Write("Maximum file size: " + RadUpload1.MaxFileSize.ToString() + " bytes<br/><br/>")
For Each f As UploadedFile In RadUpload1.InvalidFiles
Response.Write("File name: " + f.GetName() + "<br />")
Response.Write("File extension: " + f.GetExtension() + "<br />")
Response.Write("Mime-type: " + f.ContentType + "<br />")
Response.Write("File size: " + f.ContentLength.ToString() + " bytes<br />")
Next
End If
End Sub
See Also