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
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.
/App_Themes/images <-- Only .gif and .jpg
/App_Themes/css <-- Only .css
and so on...
Thanks for your reply though!
// 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.
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(this, this.GetType(), "KEY", script, true); |
} |
} break; |
case "MoveDirectory": break; |
case "CreateDirectory": break; |
case "DeleteDirectory": break; |
case "DeleteFile": break; |
case "MoveFile": break; |
} |
} |
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"};
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
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).