Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 jQuery
Hi,
 
I have a problem in uploading the file name for an image. When ever I try to Upload an image with the name having special characters, during the preview it creates problem. So I want to restrict the file upload control to restrict the entry of the special characters.
 
Here is an example of the image name that is creating the problem.
Månadens-erbjudande-CBC-sept--12
 
Here the 2nd character is creating problem for me. So I want to apply validation for the entry of the special characters in the file upload control. Any help will be appreciated. Thanks in advance.
Posted 6-Jan-13 23:15pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

To upload images to the server from Asp.Net web application, we will use the FileUpload server control. This control can be validated to upload only Image files using the RegularExpressionValidator control.
 
To achieve this drag and drop a FileUpload and RegularExpressionValidator control into your aspx page. Then set the ControlToValidate property as “FileUpload1” and the ValidationExpression property as specified below.
 
<asp:FileUpload ID="FileUpload1" runat="server" />
 
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="FileUpload1"
 ErrorMessage="Invalid Image File"
ValidationExpression=
"^([0-9a-zA-Z_\-~ :\\])+(.jpg|.JPG|.jpeg|.JPEG|.bmp|.BMP|.gif|.GIF|.png|.PNG)$">
 </asp:RegularExpressionValidator>
So the above RegularExpressionValidator will only allow jpg, jpeg, bmp, gif, png formats image files. You can add more file formats separated by “|” symbol. Also you can specify more special characters after the string “_\-~” to allow the files you want to upload to the server.
 

Good luck,
OI
  Permalink  
Comments
Orcun Iyigun at 7-Jan-13 8:47am
   
If you keep the validation expression as above it will automatically block the
What is ambigious here? I think my answer is pretty understandable. If you do not want user adding a file which contains "å" that character keep the regular expression like above. But if you change that expression to "^([0-9a-zA-Z_\-~ :\\å])+(.jpg|.JPG|.jpeg|.JPEG|.bmp|.BMP|.gif|.GIF|.png|.PNG)$"> this. user can add a file which contains "å" that character in the path. Have you tried my codeand tested it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,623
1 OriginalGriff 8,870
2 Peter Leow 4,984
3 Kornfeld Eliyahu Peter 3,300
4 Maciej Los 2,456


Advertise | Privacy | Mobile
Web01 | 2.8.150327.1 | Last Updated 7 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100