Subscribe to Subscribers
Talk to Webmaster Tony John

Forums » .NET » ASP.NET »

Regular expression for File upload control


Posted Date: 12 Jul 2012      Posted By:: Madhus     Member Level: Silver    Member Rank: 998     Points: 2   Responses: 5



Hi to all , i want to upload doc,pdf ,ppt,txt,xls and image files at a time i want to restrict .wsp,.zip,rar and other files
how can i restrict ,
if anybody knows please suggest me.

Thanks,
Madhu.




Responses

#679956    Author: Anil Kumar Pandey      Member Level: Platinum      Member Rank: 1     Date: 12/Jul/2012   Rating: 2 out of 52 out of 5     Points: 4

Here is a sample code that can validate the file extention to be uploaded using the file upload control.


<script type="text/javascript" language="javascript">
function validate() {
var fileExt= document.getElementById('<%=FileUpload1.ClientID%>').value;

var reg = /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.xls|.xlsx|.XLS|.XLSX)$/;
if (uploadcontrol.length > 0)
{

if (reg.test(fileExt))
{
return true;
}
else
{
alert("Only .Xls, .xlsx files are allowed!");
return false;
}
}
}


Thanks & Regards
Anil Kumar Pandey
Microsoft MVP, DNS MVM



 
#679959    Author: jogesh      Member Level: Gold      Member Rank: 229     Date: 12/Jul/2012   Rating: 2 out of 52 out of 5     Points: 3

You need to validate the file extensions

string fileExtension = Path.GetExtension(fileUpload.PostedFile.FileName.ToString());
//restrict .wsp,.zip,rar
if (fileExtension == ".wsp" || fileExtension == ".zip" || fileExtension == ".rar")
{
return false;
}
else
{
return true;
}



 
#679963    Author: Madhus      Member Level: Silver      Member Rank: 998     Date: 12/Jul/2012   Rating: 2 out of 52 out of 5     Points: 1

Thanks to all

its working






 
#679968    Author: Ajatshatru Upadhyay      Member Level: Gold      Member Rank: 19     Date: 12/Jul/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi,

You should use JavaScript to restrict user with certain file types.
Below is an example:


<script language="javascript" type="text/javascript">
function filterFiles() {
var path = document.getElementById('<%= FileUpload1.ClientID %>').value;

if (path.length < 1) {
alert("Please select a file");
return false;
}

var extension = path.substring(path.lastIndexOf('.') + 1).toLowerCase();

var allowedExt = new Array();
allowedExt[0] = "jpg";
allowedExt[1] = "jpeg";
allowedExt[2] = "png";
allowedExt[3] = "gif";
allowedExt[4] = "doc";
allowedExt[5] = "docx";
allowedExt[6] = "ppt";
allowedExt[7] = "pdf";
allowedExt[7] = "txt";
allowedExt[8] = "xls";
allowedExt[9] = "xlsx";

for (var i = 0; i < allowedExt.length; i++) {
if (extension == allowedExt[i]) {
return true;
}
}
alert("File type: " + extension + " is not allowed!!!");
return false;
}
</script>

<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<asp:Button ID="btnUpload" runat="server" Text="Upload"
OnClientClick="return filterFiles()" onclick="btnUpload_Click" />


Add or remove items in "allowedExt" array according to your need.

Hope it'll help you.
Regards
Ajatshatru



 
#679979    Author: chiranjita nayak      Member Level: Silver      Member Rank: 570     Date: 12/Jul/2012   Rating: 2 out of 52 out of 5     Points: 3

Very Simple way:

<asp:RegularExpressionValidator ID="valPdf" runat="server"
ErrorMessage="Only PDF files are allowed!"
ValidationExpression="\.(doc|ppt|txt|pdf|xls)$">

Display="Dynamic" ControlToValidate="uplPdf" ValidationGroup="upload" />


Hope it'll help you.
Regards
Chiranjita Nayak



 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : Modal popup extender
Previous : How to http post method in asp.net C#
Return to Discussion Forum
Post New Message
Category:

Related Messages
Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2012 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.