Edit this page

Rename a File

You can modify the name of the file on both the client and the server side of your application. The following topic will show you how to do this.

Renaming a file on the client

If you want to change the name of the file to be uploaded on the client you should handle the FileUploadStarting event. This is the last place where you can do it on the client, because after this event has been fired the upload of the file begins. Here is an example:

[C#] Example 1: Renaming a file

private int counter = 1;
private void radUpload_FileUploadStarting( object sender, FileUploadStartingEventArgs e )
{
    string newFileName = "MyCustomFileName" + counter + e.SelectedFile.File.Extension;
    e.UploadData.FileName = newFileName;
    counter++;
}

[VB.NET] Example 1: Renaming a file

Private counter As Integer = 1
Private Sub radUpload_FileUploadStarting(sender As Object, e As FileUploadStartingEventArgs)
 Dim newFileName As String = "MyCustomFileName" + counter + e.SelectedFile.File.Extension
 e.UploadData.FileName = newFileName
 counter += 1
End Sub

Renaming a file on the server

To rename a file on the server you have to override the GetFilePath() method of the RadUploadHandler. More about the RadUploadHandler can be found here.

Here is an example:

[C#] Example 2: Renaming a file on the server

private int counter = 1;
public override string GetFilePath( string fileName )
{
    string newFileName = "MyCustomFileName" + counter + fileName.Substring( fileName.LastIndexOf( '.' ) );
    return base.GetFilePath( newFileName );
}

[VB.NET] Example 2: Renaming a file on the server

Private counter As Integer = 1
Public Overloads Overrides Function GetFilePath(fileName As String) As String
 Dim newFileName As String = "MyCustomFileName" + counter + fileName.Substring(fileName.LastIndexOf("."c))
 Return MyBase.GetFilePath(newFileName)
End Function

See Also