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

How do I validate file name when I upload file in RadEditor?

3 Answers 202 Views
Editor
This is a migrated thread and some comments may be shown as answers.
Martin
Top achievements
Rank 1
Martin asked on 29 Dec 2010, 06:08 AM
I would like to validate file name when I upload document or image in RadEditor. If the file name is not valid, I would like to prompt an error message to user.

Is there any quick and easy way to do this?

I attach a screen shot to help clarify my case.

3 Answers, 1 is accepted

Sort by
0
Rumen
Telerik team
answered on 29 Dec 2010, 05:33 PM
Hello Martin,

In order to achieve the requested functionality you should implement a FileSystemContentProvider and
you will be able to get a reference to the uploaded image file, check it name and rename it if needed using the StoreFile method. Here is an example demonstrating how to create a thumbnail, which you can modify to fit your scenario:

 
using System;  
using System.Collections;  
using System.Configuration;  
using System.Data;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.HtmlControls;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using Telerik.Web.UI;  
using System.Drawing;  
using System.IO;  
   
   
public partial class static_test_editor : System.Web.UI.Page  
{  
    protected void Page_Load(object sender, EventArgs e)  
    {  
        RadEditor1.ImageManager.ViewPaths = new string[]{"~/"};  
        RadEditor1.ImageManager.UploadPaths = new string[] { "~/" };  
        RadEditor1.ImageManager.DeletePaths = 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 bool ThumbnailCallback()  
        {  
            return false;  
        }  
   
        public override string StoreFile(UploadedFile file, string path, string name, params string[] arguments)  
        {  
   
            string result = base.StoreFile(file, path, name, arguments);  
   
            System.Drawing.Image.GetThumbnailImageAbort myCallback =  
                new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);  
   
            string fileName = Path.Combine(path, name);  
            //Create a copy of the image with a different size  
            using (System.Drawing.Image originalImage = Bitmap.FromStream(file.InputStream))  
            {  
                using (System.Drawing.Image thumbnail = originalImage.GetThumbnailImage(300, 300, myCallback, IntPtr.Zero))  
                {  
                    thumbnail.Save(Context.Server.MapPath(fileName));  
                }  
            }  
   
            return result;  
        }  
    }  
}   

You can also see the following KB article on the subject: Custom FileBrowserContent Provider.

Note that the code above uses FileSystemContentProvider as a base class and not FileBrowserContentProvider. These are two different classes: FileBrowserContentProvider is the base class for all content providers and is the one with the abstract members; FileSystemContentProvider is the default content provider used by the editor - it works with the file system and has implementations for all necessary methods. This is why when you subclass the FileSystemContentProvider class, you do not need to re-define all content provider methods - just the ones you want to change. In the example above we override the StoreFile method, which is called when you upload a new file (image).



Greetings,
Rumen
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Martin
Top achievements
Rank 1
answered on 30 Dec 2010, 02:03 AM
Thank you, Rumen. It works for me.

Another question,
How can I prompt user an error message if the file name is not the one we required. I also attach a picture to help explanation.

Thank you.
0
Rumen
Telerik team
answered on 31 Dec 2010, 08:17 AM
Hello Martin,

You will be able to alert an error message using the solution provided in this KB article:
Get reference to the Page object inside a custom FileBrowserContentProvider.

Best wishes,
Rumen
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
Editor
Asked by
Martin
Top achievements
Rank 1
Answers by
Rumen
Telerik team
Martin
Top achievements
Rank 1
Share this question
or