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

Only allow some file extensions in specific folders at upload

8 Answers 1393 Views
FileExplorer
This is a migrated thread and some comments may be shown as answers.
Christian
Top achievements
Rank 1
Christian asked on 01 Jun 2010, 10:48 AM
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

8 Answers, 1 is accepted

Sort by
0
Fiko
Telerik team
answered on 03 Jun 2010, 01:22 PM
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.
0
Christian
Top achievements
Rank 1
answered on 14 Jun 2010, 10:07 AM
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
0
Accepted
Fiko
Telerik team
answered on 16 Jun 2010, 02:42 PM
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.
0
Christian
Top achievements
Rank 1
answered on 17 Jun 2010, 08:14 AM
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
            } 
        } 

0
swapnil
Top achievements
Rank 1
Iron
answered on 15 Feb 2019, 09:55 PM

I want to restrict .exe,.dll,.js file from being uploaded. Is there a way i can modify below like for same,

like allow *.* but restrict .exe, dll, js.

I dont want to include one by one file type which i can upload rather i want how certain files can be restricted from being uploaded.

Example below line allows only jpg and gif from being uploaded, i dont want to specify all the types i want rather somethink like allow *.* and restrict .exe, js, dll. I am using StoreFile() for upload.

RadFileExplorer1.Configuration.SearchPatterns = new string[] { "*.jpg" , "*.gif"};

0
Vessy
Telerik team
answered on 20 Feb 2019, 02:32 PM
Hi swapnil,

Currently RadFileExplorer provides an option to configure only the allowed file types, but not the restricted ones. A possible option you can consider is to implement a specific file types filtering following the approach from this live demo:
https://demos.telerik.com/aspnet-ajax/fileexplorer/examples/applicationscenarios/filteranddownloadfiles/defaultcs.aspx 

Regards,
Vessy
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
n/a
Top achievements
Rank 1
answered on 26 Sep 2020, 03:30 PM
I need this too! It seems like a giant oversight by not having an Excluded files type list. So strange that it is not an option? Now I have to do more work and write a bunch of code for the one task I was hoping Telerik would support. :(
0
Vessy
Telerik team
answered on 30 Sep 2020, 07:52 PM

Hi Jason,

Currently RadAsyncUpload (and respectively - RadFileExplorer) provides only option to allow the Allowed file extensions. Nevertheless, you can easily restrict the desired files by implementing client-side validation like demonstrated in the following live demo:

https://demos.telerik.com/aspnet-ajax/asyncupload/examples/validation/defaultcs.aspx

For example:

        <script>
            var $ = $telerik.$;

            function validateFail(asyncUpload, args) {
                debugger;
                var fileExtention = args.get_fileName().substring(args.get_fileName().lastIndexOf('.') + 1, args.get_fileName().length);
                if (fileExtention.indexOf("dll") > -1) {
                    alert("This file type is not supported.");
                    asyncUpload.deleteFileInputAt(args.get_rowIndex());
                    asyncUpload.deleteFileInputAt(args.get_fileName());
                }
            }

        </script>
        <telerik:RadFileExplorer
            ID="RadFileExplorer1" runat="server" Skin="Default" EnableAsyncUpload="true">
            <Configuration ViewPaths="~/images" DeletePaths="~/images" UploadPaths="~/images"
                MaxUploadFileSize="99999999" />
        </telerik:RadFileExplorer>

    protected void Page_Load(object sender, EventArgs e)
    {
        RadFileExplorer1.AsyncUpload.OnClientFileSelected = "validateFail";
    }

Regards,
Vessy
Progress Telerik

Five days of Blazor, Angular, React, and Xamarin experts live-coding on twitch.tv/CodeItLive, special prizes, and more, for FREE?! Register now for DevReach 2.0(20).

Tags
FileExplorer
Asked by
Christian
Top achievements
Rank 1
Answers by
Fiko
Telerik team
Christian
Top achievements
Rank 1
swapnil
Top achievements
Rank 1
Iron
Vessy
Telerik team
n/a
Top achievements
Rank 1
Share this question
or