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

[Solved] Give uploaded files a unique name

2 Answers 141 Views
Editor
This is a migrated thread and some comments may be shown as answers.
it-workz
Top achievements
Rank 1
it-workz asked on 28 Sep 2009, 01:21 PM
Hi,

I'm using the editor with image\flash\media manager. Works great, but I dont know how to accomplish the following:

When a user uploads a file, I want to assign the uploaded file a unique name (like a guid) . This to prevent overwriting existsing files. (user WILL check "overwrite if file exists", without caring about others useing that file.)

eg. someone uploads logo.gif, I want it to be saved as 1234567890_logo.gif

any ideas? Other methodes that lead to the same result in overwite protection are also welcome.

Thx.

2 Answers, 1 is accepted

Sort by
0
Accepted
Rumen
Telerik team
answered on 28 Sep 2009, 02:44 PM
Hi Bob,

Here is an example based on FileSystemContentProvider that will show you how to achieve your scenario:
    protected void Page_Load(object sender, EventArgs e) 
    { 
        RadEditor1.ImageManager.ViewPaths = new string[] { "~/" }; 
        RadEditor1.ImageManager.UploadPaths = new string[] { "~/" }; 
        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; 
        } 
    } 



Best wishes,
Rumen
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
it-workz
Top achievements
Rank 1
answered on 28 Sep 2009, 03:10 PM

Hi Rumen,

Exxelent answer, exactly what i needed. I'd suggest you put it in the knowledgebase.

Here's the VB translation:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    RadEditor1.ImageManager.ViewPaths = New String() {"~/"}
    RadEditor1.ImageManager.UploadPaths = New String() {"~/"}
    RadEditor1.ImageManager.ContentProviderTypeName = GetType(myprovider).AssemblyQualifiedName
End Sub

Public Class myprovider
    Inherits Telerik.Web.UI.Widgets.FileSystemContentProvider
   
    Public Sub New(ByVal context As HttpContext, ByVal searchPatterns As String(), ByVal viewPaths As String(), ByVal uploadPaths As String(), ByVal deletePaths As String(), ByVal selectedUrl As String, _
    ByVal selectedItemTag As String)
        MyBase.New(context, searchPatterns, viewPaths, uploadPaths, deletePaths, selectedUrl, _
        selectedItemTag)
    End Sub
   
    Public Overloads Overrides Function StoreFile(ByVal file As UploadedFile, ByVal path As String, ByVal name As String, ByVal ParamArray arguments As String()) As String
        Dim fileExtention As String = name.Substring(name.LastIndexOf("."))
        Dim fileNameWithoutExtension As String = name.Substring(0, name.IndexOf("."))
        Dim newFileName As String = fileNameWithoutExtension & DateTime.UtcNow.Ticks & fileExtention
       
        Dim result As String = MyBase.StoreFile(file, path, newFileName, arguments)
       
        Return result
    End Function
End Class

Tags
Editor
Asked by
it-workz
Top achievements
Rank 1
Answers by
Rumen
Telerik team
it-workz
Top achievements
Rank 1
Share this question
or