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 TargetFolder
In order to use the automatic file saving, the TargetFolder property must be set to an existing virtual path. This virtual path will be internally resolved to a physical path by calling the Server.MapPath() method.
The TargetPhysicalFolder property is provided for the cases where the files must be automatically saved to a physical path. If you set both TargetFolder and TargetPhysicalFolder properties, RadUpload will use TargetPhysicalFolder.
When one of the properties above was set, the uploaded files will be validated if the validation has been enabled. Then the valid files will be saved in the specified location with their original names from the client computer. If a file with the same name already exists on the server it may be overwritten, depending the value of the OverwriteExistingFiles property.
The example below demonstrates how to implement automatic file saving. The uploaded files will be saved in the "~/MyFiles" folder, overwriting the existing files:
<rad:radupload ID="RadUpload1" Runat="server" OverwriteExistingFiles="true" TargetFolder="~/MyFiles" /> <asp:button Runat="server" ID="Button1" Text="Submit" /> |
The FileExists event
The FileExists event is fired when one of the TargetFolder or TargetPhysicalFolder properties was set, the OverwriteExistingFiles property was set to false and there is already a file with the same name as the currently uploaded file.
You can use this event to save the existing uploaded files with a different name.
The example below demonstrates how to rename the existing files by handling the FileExists event:
<rad:radupload ID="RadUpload1" Runat="server" OverwriteExistingFiles="false" TargetFolder="~/MyFiles" OnFileExists="RadUpload1_FileExists" /> <asp:button Runat="server" ID="Button1" Text="Submit" /> |
VB.NET
Imports Telerik.WebControls ... Protected Sub RadUpload1_FileExists(ByVal sender As Object, ByVal e As UploadedFileEventArgs) Dim counter As Integer = 1
Dim file As UploadedFile = e.UploadedFile
Dim targetFolder As String = Server.MapPath(RadUpload1.TargetFolder)
Dim targetFileName As String = System.IO.Path.Combine(targetFolder, _ file.GetNameWithoutExtension & counter & file.GetExtension)
While System.IO.File.Exists(targetFileName) counter += 1 targetFileName = System.IO.Path.Combine(targetFolder, _ file.GetNameWithoutExtension & counter & file.GetExtension) End While
file.SaveAs(targetFileName) End Sub |
C#
using Telerik.WebControls; ... protected void RadUpload1_FileExists(object sender, UploadedFileEventArgs e) { int counter = 1;
UploadedFile file = e.UploadedFile;
string targetFolder = Server.MapPath(RadUpload1.TargetFolder);
string targetFileName = System.IO.Path.Combine(targetFolder, file.GetNameWithoutExtension() + counter.ToString() + file.GetExtension());
while (System.IO.File.Exists(targetFileName)) { counter ++; targetFileName = System.IO.Path.Combine(targetFolder, file.GetNameWithoutExtension() + counter.ToString() + file.GetExtension()); }
file.SaveAs(targetFileName); } |