Rename file on upload

2 posts, 1 answers
  1. mkerchenski
    mkerchenski avatar
    8 posts
    Member since:
    Jun 2012

    Posted 20 Mar 2013 Link to this post

    Is it possible to rename file during the upload, if the file with the same name already exists. 
  2. Answer
    Vessy avatar
    2129 posts

    Posted 22 Mar 2013 Link to this post

    Hi Mike,

    I have just answered you support ticket on the subject, for convenience I am pasting my answer here as well:

    You could achieve the desired behavior by subclassing the the FileExplorer's built-in FileSystemContentProvider where to override its StoreFile() method. More information on how to do it is available in the following help article: Using custom FileBrowserContentProvider

    In the override of the
    StoreFile(), you could check whether a file with such a name already exists in the current folder and to rename the uploaded file in this case:
    public override string StoreFile(Telerik.Web.UI.UploadedFile file, string path, string name, params string[] arguments)
        var currentDir = base.ResolveDirectory(path);
        foreach (FileItem fileInDir in currentDir.Files)
            if (fileInDir.Name == name)
                name = name.Substring(0, name.LastIndexOf('.')) + new Random().Next() + name.Substring(name.LastIndexOf('.'));;
        return base.StoreFile(file, path, name, arguments);

    To make the above functionality work properly, you will need to ensure that the "Overwrite if file exists?" checkbox of the upload dialog is always checked. You could achieve this in the handler of the FileExplorer's OnClientLoad event in a similar way:
    <telerik:RadFileExplorer ID="fileexplorer1" runat="server" OnClientLoad="OnClientLoad">
        <Configuration ViewPaths="~/Images" DeletePaths="~/Images" UploadPaths="~/Images" />
    <script type="text/javascript">
        function OnClientLoad(explorer, args) {
            var chkBox = $get("fileexplorer1_chkOverwrite");
            chkBox.checked = true;
   = "none";

    For your convenience I am attaching my test project, describing the approach. I hope it would be helpful for you.

    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Back to Top