This is a migrated thread and some comments may be shown as answers.

Rename file on upload

1 Answer 215 Views
FileExplorer
This is a migrated thread and some comments may be shown as answers.
mkerchenski
Top achievements
Rank 1
mkerchenski asked on 20 Mar 2013, 08:10 PM
Is it possible to rename file during the upload, if the file with the same name already exists. 

1 Answer, 1 is accepted

Sort by
0
Accepted
Vessy
Telerik team
answered on 22 Mar 2013, 03:02 PM
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('.'));;
            break;
        }
    }
 
    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" />
</telerik:RadFileExplorer>
<script type="text/javascript">
    function OnClientLoad(explorer, args) {
        var chkBox = $get("fileexplorer1_chkOverwrite");
        chkBox.checked = true;
        chkBox.parentNode.style.display = "none";
    }
</script>

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


Regards,
Vesi
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.
Tags
FileExplorer
Asked by
mkerchenski
Top achievements
Rank 1
Answers by
Vessy
Telerik team
Share this question
or