New to Telerik UI for ASP.NET AJAX? Download free 30-day trial

Giving the uploaded files unique names

HOW-TO

Give the uploaded files unique names

DESCRIPTION

When a user uploads a file, he can overwrite an existing file by clicking on the "overwrite if file exists" checkbox, without caring about others using that file. It is possible to prevent this problem by assigning the uploaded file an unique name (like a guid).

SOLUTION

Here is a code example based on FileSystemContentProvider that shows how to rename the uploaded images:

    protected void Page_Load(object sender, EventArgs e) 
    { 
        RadEditor1.ImageManager.ViewPaths = new string[] { "~/Images" }; 
        RadEditor1.ImageManager.UploadPaths = new string[] { "~/Images" }; 
        RadEditor1.ImageManager.ContentProviderTypeName = typeof(myprovider).AssemblyQualifiedName; 
    } 

    public class myprovider : Telerik.Web.UI.Widgets.FileSystemContentProvider 
    { 

        public myprovider(HttpContext context, string[] searchPatterns, string[] viewPaths, string[] uploadPaths, string[] deletePaths, string selectedUrl, string selectedItemTag) 
            : base(context, searchPatterns, viewPaths, uploadPaths, deletePaths, selectedUrl, selectedItemTag) 
        { 
        } 

        public override string StoreFile(UploadedFile file, string path, string name, params string[] arguments) 
        { 
            string fileExtention = name.Substring(name.LastIndexOf(".")); 
            string fileNameWithoutExtension = name.Substring(0,name.IndexOf(".")); 
            string newFileName = fileNameWithoutExtension + DateTime.UtcNow.Ticks + fileExtention; 

            string result = base.StoreFile(file, path, newFileName, arguments); 

            return result; 
        } 
    }
In this article
Not finding the help you need? Improve this article