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

Restrict Files

5 Answers 118 Views
Upload (Obsolete)
This is a migrated thread and some comments may be shown as answers.
Jim
Top achievements
Rank 2
Jim asked on 27 May 2009, 12:41 PM
Does anyone know of a way to restrict the file extensions that can be uploaded.  I don't want users to upload .exe, .vbs, .bat, etc., but any other files would be acceptable.  Can this be done, either with the control or in java?

Thanks,

Jim

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 27 May 2009, 01:33 PM
Hi jim,

    You can set the AllowedFileExtensions property which lists the valid file extensions for uploaded files. Checkout the following links for more information.
Integrated Validation

    The following example shows how to prevent the postback if the files to upload are invalid.
OnClientSubmitting

Hope this helps
-Princy.
0
Jim
Top achievements
Rank 2
answered on 27 May 2009, 04:18 PM
Sorry I was unclear.  I have used the allowfileextensions before, but I just want to block a handful of harmful files and allow everything else, this could be hundreds of file extensions.  It would be a pain and quite a problem to list all possible file extensions, easier to block the handful of possible problem causing files.

Thanks,
Jim
0
Veselin Vasilev
Telerik team
answered on 28 May 2009, 09:24 AM
Hi Jim,

You can use a CustomValidator control and in its ClientValidationFunction you can check if the extensions of the selected files are blocked (exe, pif, etc.). Based on these checkings you can invalidate the result.

All the best,
Veselin Vasilev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Jim
Top achievements
Rank 2
answered on 28 May 2009, 11:27 AM
I used the following java script based off of one I found on your site to accomplish what I need.

function

 

ValidateSubmission(source, eventArgs)

 

{

 

var upload = $find("<%= ruUpload.ClientID %>");

 

 

var inputs = upload.getFileInputs();

 

 

var cont = 'true';

 

 

 

for (i = 0; i < inputs.length; i++)

 

{

 

var inp = inputs[i].value;

 

 

if (inp.indexOf('.exe') > -1 ||

 

inp.indexOf(

'.vbs') > -1 ||

 

inp.indexOf(

'.bat') > -1)

 

cont =

'false'

 

}

 

if(cont == 'false')

 

{

 

var cnt = inputs.length;

 

 

for(i = 0; i<cnt;i++)

 

{

upload.clearFileInputAt(i);

}

alert(

"Submission blocked: one or more files have the wrong type! File extension .exe, .vbs and .bat not allowed.");

 

}

 

else

 

{

 

var exts = upload.get_allowedFileExtensions();

 

 

if(exts.length > 0)

 

{

eventArgs.set_cancel(!upload.validateExtensions());

 

if (eventArgs.get_cancel())

 

{

 

for(i=0;i<inputs.length;i++)

 

{

upload.clearFileInputAt(i);

}

alert(

"Submission blocked: one or more files have the wrong type!");

 

}

}

}

}



Thanks for all of your help.
0
Basel Nimer
Top achievements
Rank 2
answered on 02 Nov 2009, 01:11 PM
any idea why my ClientValidationFunction is not firing at all for the same scenario? i want to force having at least one file in radUpload.

<asp:CustomValidator ID="cvUploader" runat="server"  Display=Dynamic ClientValidationFunction="ClientValidateUpload"  ErrorMessage="At least one file should be selected."></asp:CustomValidator>
<telerik:RadScriptBlock ID="RadScriptBlock3" runat="server">
<script language="javascript" type="text/javascript">  
function ClientValidateUpload(source, arguments)
{
    u = $find(uTelUploaderID);
    if (u.getFileInputs().length === 1)
    {
        arguments.IsValid = false;
    }
}       
 
</script>
</telerik:RadScriptBlock>   

uTelUploaderID holds the clientID  of the RadUpload.the event is not firing at all. and i am not using server validate.
Tags
Upload (Obsolete)
Asked by
Jim
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
Jim
Top achievements
Rank 2
Veselin Vasilev
Telerik team
Basel Nimer
Top achievements
Rank 2
Share this question
or