Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

1 solution

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.

C#
<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
 
Share this answer
 
Comments
[no name] 7-Jan-13 8:10am    
As I said in my question I need to restrict the special characters for different languages. How many shall I add there? e.g Månadens, here "å" is a special character. similarly how many shall I define?
Orcun Iyigun 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900