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.
Validation
RadUpload can automatically validate the size, the extension and the mime-type of the uploaded files against your preferences. It is only necessary to set the values of the needed RadUpload validation properties:
- Set the AllowedFileExtensions property to enable the file extension validation. You can set multiple file extensions if you set the value of this property to a comma separated list in the ASPX or a string array in the code-behind. You can also easily validate the file extensions client-side. The following example demonstrates how to set multiple file extensions in the ASPX and in the code-behind:
ASPX
|
<rad:radupload id="RadUpload1" runat="server" allowedfileextensions=".zip,.jpg,.jpeg" />
|
VB.NET
|
RadUpload1.AllowedFileExtensions = New String() {".zip", ".jpg", ".jpeg"}
|
C#
|
RadUpload1.AllowedFileExtensions = new string[] {".zip", ".jpg", ".jpeg"};
|
Note: you must include the leading dot (.) in the file extension.
-
Set the
AllowedMimeTypes property to enable the mime-type validation.
Note: there are several file extensions that have multiple mime-types associated with them, and the different browsers send different mime-type for the same file. For these types of files you need to set all associated mime-types of the file extension in order to ensure the proper validation. You can set multiple mime-types the same way as setting multiple file extensions.
- Set the MaxFileSize property a to enable the file size validation. The value of this property specifies the maximal allowed uploaded file size in bytes.
The valid files can be accessed using the UploadedFiles property. The invalid files can be accessed using the InvalidFiles property if needed.
After the validaton the valid files will be automatically saved to the specified location if one of the TargetFolder or TargetPhysicalFolder properties has been set.
This example demonstrates how to configure RadUpload to receive only ZIP files with size less than 1000000 bytes. Note that RadUpload is configured to allow all mime-types associated with the .zip file extension. The valid files will be automatically saved to the "~/My files" folder.
ASPX:
|
<rad: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" />
|
Why are my files considered invalid?
Sometimes some files are considered invalid, even if they seem valid. You can visually check the reasons for this using the following code:
VB.NET
|
Imports Telerik.WebControls ... Protected Sub Button1_Click(ByVal sender As Object, ByVal e as EventArgs) Response.Write("Settings of " & RadUpload1.ID & "<br/>") Response.Write("AllowedMimeTypes " & String.Join(",", RadUpload1.AllowedMimeTypes) & "<br/>") Response.Write("AllowedFileExtensions " & String.Join(",", RadUpload1.AllowedFileExtensions) & "<br/>") Response.Write("MaxFileSize " & RadUpload1.MaxFileSize & " 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 & " bytes<br />") Next End Sub
|
C#
|
using Telerik.WebControls; ... protected void Button1_Click(object sender, EventArgs e) { Response.Write("Settings of " + RadUpload1.ID + "<br/>"); Response.Write("AllowedMimeTypes " + string.Join(",", RadUpload1.AllowedMimeTypes) + "<br/>"); Response.Write("AllowedFileExtensions " + string.Join(",", RadUpload1.AllowedFileExtensions) + "<br/>"); Response.Write("MaxFileSize " + RadUpload1.MaxFileSize + " 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 + " bytes<br />"); } }
|