Only allow some file extensions in specific folders at upload

5 posts, 1 answers
  1. Christian
    Christian avatar
    9 posts
    Member since:
    Jun 2010

    Posted 01 Jun 2010 Link to this post

    Hi!

    I use the file explorer and have a problem I can't find a solution for.
    In my file explorer I list my App_Themes folder. Is there a way to set file extensions that are allowed in specific folders at upload?
    I just want to enable upload of .css-files in the /css folder and only images such as .jpg, .gif, .png etc in the /images folder.

    Is there functionality in file explorer or upload control to help me with my problem.

    Maybe I can use some of the javascript events, but I hope there is a simpler solution.

    Regards

    Christian Persson
  2. Fiko
    Admin
    Fiko avatar
    1406 posts

    Posted 03 Jun 2010 Link to this post

    Hi Christian,

    You need to set the desired extensions in the SerchPaterns property of the RadFileExplorer control. This is an example which shows how to allow uploaded only for files with .JPG and .GIF extensions:
    RadFileExplorer1.Configuration.SearchPatterns = new string[] { "*.jpg" , "*.gif"};

    I hope this helps.

    Best wishes,
    Fiko
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Christian
    Christian avatar
    9 posts
    Member since:
    Jun 2010

    Posted 14 Jun 2010 Link to this post

    I understand but that sets what file extensions that are allowed overall in my fileexplorer. I need to restrict upload of extensions in specific folders.
    /App_Themes/images <-- Only .gif and .jpg
    /App_Themes/css <-- Only .css
    and so on...

    Thanks for your reply though!

    // Christian
  5. Answer
    Fiko
    Admin
    Fiko avatar
    1406 posts

    Posted 16 Jun 2010 Link to this post

    Hello Christian,

    In your scenario I recommend you to attach a handler to the OnItemCommand event of the RadFileExplorer and implement the handler as shown bellow:
    protected void RadFileExplorer1_ItemCommand(object sender, RadFileExplorerEventArgs e)
    {
        switch (e.Command)
        {
            case "UploadFile":
                {
                    string path = e.Path;// Path to the file on the server (virtual path)
                          // Your logic that checks the file type
                    if (path.EndsWith(".jpg"))
                    {// Cannot upload *.jpg files
                        e.Cancel = true;
     
                        string script = "alert('You cannot upload .JPg files to this folder');";
                                 // Show javascript alert
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "KEY", script, true);
                    }
                } break;
            case "MoveDirectory": break;
            case "CreateDirectory": break;
            case "DeleteDirectory": break;
            case "DeleteFile": break;
            case "MoveFile": break;
        }
    }


    Greetings,
    Fiko
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  6. Christian
    Christian avatar
    9 posts
    Member since:
    Jun 2010

    Posted 17 Jun 2010 Link to this post

    Thank you Fiko! You really saved my day! :)

    It works like a charm! I decided to share my code if it maybe can help someone else. It's a bit messy but you should get the point. :p

            protected void rfeFileExplorer_ItemCommand(object sender, RadFileExplorerEventArgs e) 
            { 
                switch (e.Command) 
                { 
                    case "UploadFile"
                        { 
                            string path = e.Path; 
     
                            // Extensions allowed in the different subfolders of /App_Themes/Themename/ 
                            string[] allowedCommonExtensions = new string[] { ".jpg"".gif"".png"".js"".xml"".sfw"".css"".txt" }; 
                            string[] allowedImageExtensions = new string[] { ".jpg"".gif"".png" }; 
                            string[] allowedCssExtensions = new string[] { ".css" }; 
     
                            // Get what extension user try to upload 
                            string extension = path.Substring(path.LastIndexOf(".")); 
                            bool extensionIsAllowed = false
     
     
                            if (path.IndexOf("/App_Themes/Standard/images/") > 0) 
                            { 
                                // If subfolder is images, just allow ".jpg", ".gif", ".png" 
                                foreach (string s in allowedImageExtensions) 
                                { 
                                    if (extension == s) 
                                    { 
                                        extensionIsAllowed = true
                                        break
                                    } 
                                } 
                            } 
                            else if (path.IndexOf("/App_Themes/Standard/css/") > 0) 
                            { 
                                // If subfolder is css, just allow ".css" 
                                foreach (string s in allowedCssExtensions) 
                                { 
                                    if (extension == s) 
                                    { 
                                        extensionIsAllowed = true
                                        break
                                    } 
                                } 
                            } 
                            else if (path.IndexOf("/App_Themes/{0}/common/") > 0) 
                            { 
                                // If subfolder is common, just allow ".jpg", ".gif", ".png", ".js", ".xml", ".sfw", ".css", ".txt" 
                                foreach (string s in allowedCommonExtensions) 
                                { 
                                    if (extension == s) 
                                    { 
                                        extensionIsAllowed = true
                                        break
                                    } 
                                } 
                            } 
     
                            // File is not allowed in current subfolder, display message to user 
                            if (!extensionIsAllowed) 
                            { 
                                e.Cancel = true
     
                                string strSubFolder = path.Substring("/admin/App_Themes/Standard/".Length); 
                                int indexOfFirstSubFolderSlash = path.Substring("/admin/App_Themes/Standard/".Length).IndexOf("/"); 
     
                                // This gets what subfolder you are in, i.e css, images or common 
                                string currentFolder = strSubFolder.Substring(0, indexOfFirstSubFolderSlash); 
     
                                string script = "alert('Files with extension " + extension + " is not allowed in the " + currentFolder + " folder');"
                                ScriptManager.RegisterStartupScript(thisthis.GetType(), "KEY", script, true); 
                            } 
     
                        } break
                    case "MoveDirectory"break
                    case "CreateDirectory"break
                    case "DeleteDirectory"break
                    case "DeleteFile"break
                    case "MoveFile"break
                } 
            } 

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017