This message was discovered on ASPFriends.com 'aspngregexp' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
| Julian Voelcker |
I want to provide a facility for users to upload .gif, .jpeg, .jpg and bmp images to a site and then to extract the file name from a full path (e.g. C:\temp\image.gif).
I need a regular expression to validate that the user has selected an suitable image to upload and that the path to it is a valid file path.
Anyone here willing to provide some pointers?
Cheers,
Julian Voelcker The Virtual World (UK) Limited Cirencester, United Kingdom
|
|
| |
| |
| Terry Voss |
I use these:
Public Class Utility
Public Shared Function JustDrive(ByVal cPath As String) As String Dim lcJustDrive As String = Directory.GetDirectoryRoot(cPath) Return lcJustDrive.Replace("\", "") End Function
Public Shared Function JustExt(ByVal cFileName As String) As String Dim fi As FileInfo = New FileInfo(cFileName) Return fi.Extension End Function
Public Shared Function JustFName(ByVal cFileName As String) As String Dim fi As FileInfo = New FileInfo(cFileName) Return fi.Name End Function
Public Shared Function JustPath(ByVal cPath As String) As String Dim lcPath As String = cPath.Trim() If lcPath.IndexOf("\"c) = -1 Then Return "" Else Return lcPath.Substring(0, lcPath.LastIndexOf("\"c)) End If End Function
Public Shared Function JustStem(ByVal cPath As String) As String Dim lcFileName As String = JustFName(cPath.Trim()) If lcFileName.IndexOf(".") = -1 Then Return lcFileName Else Return lcFileName.Substring(0, lcFileName.LastIndexOf("."c)) End If End Function
End Class
Terry Voss Developer/Owner Computer Consulting Microsoft Certified Partner http://www.computer-consulting.com Click here to reveal e-mail address 2403 North Nettleton Street Spokane WA 99205 Tel: 509-327-7202 Fax: 509-327-2303 http://www.spokaneoutdoors.com
-----Original Message----- From: Julian Voelcker [mailto:Click here to reveal e-mail address] Sent: Tuesday, July 02, 2002 7:48 AM To: aspngregexp Subject: [aspngregexp] Validating and extracting filenames
I want to provide a facility for users to upload .gif, .jpeg, .jpg and bmp images to a site and then to extract the file name from a full path (e.g. C:\temp\image.gif).
I need a regular expression to validate that the user has selected an suitable image to upload and that the path to it is a valid file path.
Anyone here willing to provide some pointers?
Cheers,
Julian Voelcker The Virtual World (UK) Limited Cirencester, United Kingdom
| [aspngregexp] member Click here to reveal e-mail address = YOUR ID | http://www.asplists.com/asplists/aspngregexp.asp = JOIN/QUIT | http://www.asplists.com/search = SEARCH Archives
|
|
| |
|
| |
| dave wanta (VIP) |
I don't know that I would use the regex class for something like that... Between a combination of System.IO and a string array, you should have what you need.
just going from memory here, but this should work.. using System.IO;
string[] GoodExt = { '.gif','.jpeg','.jpg'}; string UploadedImageName = File1.PostedFile.FileName; //the uploaded name bool Found = false; if ( Path.HasExtension( UploadedImageName ) ){
foreach ( string s in GoodExt ){ if ( s.ToUpper() == Path.GetExtension( UploadedImageName ).ToUpper() ){ Found = true; break; }
}
}
if ( Found ){ Response.Write ("good image"); } else{ Response.Write ("nope, bad image"); }
----- Original Message ----- From: "Julian Voelcker" <Click here to reveal e-mail address> To: "aspngregexp" <Click here to reveal e-mail address> Sent: Tuesday, July 02, 2002 9:47 AM Subject: [aspngregexp] Validating and extracting filenames
[Original message clipped]
|
|
| |
|
| |
| Julian Voelcker |
Thanks, but I would like to do the validation before they attempt to upload the image by checking what is selected using the RegularExpression Validator.
How would I go about this using Regular Expressions Cheers,
Julian Voelcker The Virtual World (UK) Limited Cirencester, United Kingdom On Tue, 2 Jul 2002 14:02:25 -0500, Dave wanta wrote: [Original message clipped]
|
|
| |
|
| |
| Carlos Magalhaes |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"> <HTML> <HEAD> <META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; = charset=3Dus-ascii"> <META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version = 5.5.2652.35"> <TITLE>RE: [aspngregexp] RE: Validating and extracting = filenames</TITLE> </HEAD> <BODY>
<P><FONT SIZE=3D2>Hi,</FONT> </P>
<P><FONT SIZE=3D2>How can you call those functions from another = class?</FONT> </P>
<P><FONT SIZE=3D2>CM</FONT> </P>
<P><FONT SIZE=3D2> </FONT> </P>
<P><FONT SIZE=3D2>-----Original Message-----</FONT> <BR><FONT SIZE=3D2>From: Terry Voss [<A = HREF=3D"mailto:Click here to reveal e-mail address">mailto:tvoss@computer-cons= ulting.com</A>] </FONT> <BR><FONT SIZE=3D2>Sent: Tuesday, July 02, 2002 6:09 PM</FONT> <BR><FONT SIZE=3D2>To: aspngregexp</FONT> <BR><FONT SIZE=3D2>Subject: [aspngregexp] RE: Validating and extracting = filenames</FONT> </P>
<P><FONT SIZE=3D2>I use these: </FONT> </P>
<P><FONT SIZE=3D2>Public Class Utility</FONT> </P>
<P> = <FONT SIZE=3D2>Public Shared = Function JustDrive(ByVal cPath As String) As String</FONT> <BR> = = <FONT SIZE=3D2>Dim = lcJustDrive As String =3D Directory.GetDirectoryRoot(cPath)</FONT> <BR> = = <FONT SIZE=3D2>Return = lcJustDrive.Replace("\", "")</FONT> <BR> = <FONT SIZE=3D2>End = Function</FONT> </P>
<P> = <FONT SIZE=3D2>Public Shared = Function JustExt(ByVal cFileName As String) As String</FONT> <BR> = = <FONT SIZE=3D2>Dim fi As = FileInfo =3D New FileInfo(cFileName)</FONT> <BR> = = <FONT SIZE=3D2>Return = fi.Extension</FONT> <BR> = <FONT SIZE=3D2>End = Function</FONT> </P>
<P> = <FONT SIZE=3D2>Public Shared = Function JustFName(ByVal cFileName As String) As String</FONT> <BR> = = <FONT SIZE=3D2>Dim fi As = FileInfo =3D New FileInfo(cFileName)</FONT> <BR> = = <FONT SIZE=3D2>Return = fi.Name</FONT> <BR> = <FONT SIZE=3D2>End = Function</FONT> </P>
<P> = <FONT SIZE=3D2>Public Shared = Function JustPath(ByVal cPath As String) As String</FONT> <BR> = = <FONT SIZE=3D2>Dim lcPath As = String =3D cPath.Trim()</FONT> <BR> = = <FONT SIZE=3D2>If = lcPath.IndexOf("\"c) =3D -1 Then</FONT> <BR> = = = <FONT SIZE=3D2>Return = ""</FONT> <BR> = = <FONT SIZE=3D2>Else</FONT> <BR> = = = <FONT SIZE=3D2>Return = lcPath.Substring(0, lcPath.LastIndexOf("\"c))</FONT> <BR> = = <FONT SIZE=3D2>End If</FONT> <BR> = <FONT SIZE=3D2>End = Function</FONT> </P>
<P> = <FONT SIZE=3D2>Public Shared = Function JustStem(ByVal cPath As String) As String</FONT> <BR> = = <FONT SIZE=3D2>Dim = lcFileName As String =3D JustFName(cPath.Trim())</FONT> <BR> = = <FONT SIZE=3D2>If = lcFileName.IndexOf(".") =3D -1 Then</FONT> <BR> = = = <FONT SIZE=3D2>Return = lcFileName</FONT> <BR> = = <FONT SIZE=3D2>Else</FONT> <BR> = = = <FONT SIZE=3D2>Return = lcFileName.Substring(0, lcFileName.LastIndexOf("."c))</FONT> <BR> = = <FONT SIZE=3D2>End If</FONT> <BR> = <FONT SIZE=3D2>End = Function</FONT> </P>
<P> <FONT SIZE=3D2>End = Class</FONT> </P> <BR>
<P><FONT SIZE=3D2>Terry Voss</FONT> <BR><FONT SIZE=3D2>Developer/Owner</FONT> <BR><FONT SIZE=3D2>Computer Consulting</FONT> <BR><FONT SIZE=3D2>Microsoft Certified Partner</FONT> <BR><FONT SIZE=3D2><A HREF=3D"http://www.computer-consulting.com" = TARGET=3D"_blank">http://www.computer-consulting.com</A></FONT> <BR><FONT SIZE=3D2>Click here to reveal e-mail address</FONT> <BR><FONT SIZE=3D2>2403 North Nettleton Street</FONT> <BR><FONT SIZE=3D2>Spokane WA 99205</FONT> <BR><FONT SIZE=3D2>Tel: 509-327-7202</FONT> <BR><FONT SIZE=3D2>Fax: 509-327-2303</FONT> <BR><FONT SIZE=3D2><A HREF=3D"http://www.spokaneoutdoors.com"" target="_blank">http://www.spokaneoutdoors.com" = TARGET=3D"_blank">http://www.spokaneoutdoors.com</A></FONT> </P> <BR>
<P><FONT SIZE=3D2>-----Original Message-----</FONT> <BR><FONT SIZE=3D2>From: Julian Voelcker [<A = HREF=3D"mailto:Click here to reveal e-mail address">mailto:Click here to reveal e-mail address</A>]</FONT> <BR><FONT SIZE=3D2>Sent: Tuesday, July 02, 2002 7:48 AM</FONT> <BR><FONT SIZE=3D2>To: aspngregexp</FONT> <BR><FONT SIZE=3D2>Subject: [aspngregexp] Validating and extracting = filenames</FONT> </P>
<P><FONT SIZE=3D2>I want to provide a facility for users to upload = .gif, .jpeg, .jpg and</FONT> <BR><FONT SIZE=3D2>bmp images to a site and then to extract the file = name from a full</FONT> <BR><FONT SIZE=3D2>path (e.g. C:\temp\image.gif).</FONT> </P>
<P><FONT SIZE=3D2>I need a regular expression to validate that the user = has selected an</FONT> <BR><FONT SIZE=3D2>suitable image to upload and that the path to it is = a valid file path.</FONT> </P>
<P><FONT SIZE=3D2>Anyone here willing to provide some pointers?</FONT> </P>
<P><FONT SIZE=3D2>Cheers,</FONT> </P>
<P><FONT SIZE=3D2>Julian Voelcker</FONT> <BR><FONT SIZE=3D2>The Virtual World (UK) Limited</FONT> <BR><FONT SIZE=3D2>Cirencester, United Kingdom</FONT> </P> <BR> <BR>
<P><FONT SIZE=3D2>| [aspngregexp] member Click here to reveal e-mail address = =3D YOUR ID</FONT> <BR><FONT SIZE=3D2>| <A = HREF=3D"http://www.asplists.com/asplists/aspngregexp.asp"" target="_blank">http://www.asplists.com/asplists/aspngregexp.asp" = TARGET=3D"_blank">http://www.asplists.com/asplists/aspngregexp.asp</A> = =3D JOIN/QUIT</FONT> <BR><FONT SIZE=3D2>| <A HREF=3D"http://www.asplists.com/search" = TARGET=3D"_blank">http://www.asplists.com/search</A> =3D SEARCH = Archives </FONT> </P> <BR>
<P><FONT SIZE=3D2>| [aspngregexp] member Click here to reveal e-mail address =3D YOUR = ID</FONT> <BR><FONT SIZE=3D2>| <A = HREF=3D"http://www.asplists.com/asplists/aspngregexp.asp"" target="_blank">http://www.asplists.com/asplists/aspngregexp.asp" = TARGET=3D"_blank">http://www.asplists.com/asplists/aspngregexp.asp</A> = =3D JOIN/QUIT</FONT> <BR><FONT SIZE=3D2>| <A HREF=3D"http://www.asplists.com/search" = TARGET=3D"_blank">http://www.asplists.com/search</A> =3D SEARCH = Archives</FONT> </P>
</BODY> </HTML>
------------------------------------------------------------- This email and any files transmitted are confidential and intended solely for the use of the individual or entity to which they are addressed, whose privacy should be respected. Any views or opinions are solely those of the author and do not necessarily represent those of the Trencor Group, or any of its representatives, unless specifically stated.
Email transmission cannot be guaranteed to be secure, error free or without virus contamination. The sender therefore accepts no liability for any errors or omissions in the contents of this message, nor for any virus infection that might result from opening this message. Trencor is not responsible in the event of any third party interception of this email.
If you have received this email in error please notify Click here to reveal e-mail address For more information about Trencor, visit www.trencor.net <http://www.trencor.net>
|
|
| |
|
| |
| Terry Voss |
Carlos,
Put them in .vb file in your project .
Then
Instantiate utility class anywhere else and use methods of class.
Dim x as utility = new utility() Dim ext as string = x.justext(fullpath)
Terry Voss Developer/Owner Computer Consulting Microsoft Certified Partner http://www.computer-consulting.com <http://www.computer-consulting.com/> Click here to reveal e-mail address 2403 North Nettleton Street Spokane WA 99205 Tel: 509-327-7202 Fax: 509-327-2303 http://www.spokaneoutdoors.com <http://www.spokaneoutdoors.com/>
-----Original Message----- From: Carlos Magalhaes [mailto:Click here to reveal e-mail address] Sent: Wednesday, July 03, 2002 12:09 AM To: aspngregexp Subject: [aspngregexp] RE: Validating and extracting filenames
Hi, How can you call those functions from another class? CM
-----Original Message----- From: Terry Voss [ mailto:Click here to reveal e-mail address] Sent: Tuesday, July 02, 2002 6:09 PM To: aspngregexp Subject: [aspngregexp] RE: Validating and extracting filenames I use these: Public Class Utility Public Shared Function JustDrive(ByVal cPath As String) As String Dim lcJustDrive As String Directory.GetDirectoryRoot(cPath) Return lcJustDrive.Replace("\", "") End Function Public Shared Function JustExt(ByVal cFileName As String) As String Dim fi As FileInfo = New FileInfo(cFileName) Return fi.Extension End Function Public Shared Function JustFName(ByVal cFileName As String) As String Dim fi As FileInfo = New FileInfo(cFileName) Return fi.Name End Function Public Shared Function JustPath(ByVal cPath As String) As String Dim lcPath As String = cPath.Trim() If lcPath.IndexOf("\"c) = -1 Then Return "" Else Return lcPath.Substring(0, lcPath.LastIndexOf("\"c)) End If End Function Public Shared Function JustStem(ByVal cPath As String) As String Dim lcFileName As String = JustFName(cPath.Trim()) If lcFileName.IndexOf(".") = -1 Then Return lcFileName Else Return lcFileName.Substring(0, lcFileName.LastIndexOf("."c)) End If End Function End Class
Terry Voss Developer/Owner Computer Consulting Microsoft Certified Partner http://www.computer-consulting.com Click here to reveal e-mail address 2403 North Nettleton Street Spokane WA 99205 Tel: 509-327-7202 Fax: 509-327-2303 http://www.spokaneoutdoors.com
-----Original Message----- From: Julian Voelcker [ mailto:Click here to reveal e-mail address] Sent: Tuesday, July 02, 2002 7:48 AM To: aspngregexp Subject: [aspngregexp] Validating and extracting filenames I want to provide a facility for users to upload .gif, .jpeg, .jpg and bmp images to a site and then to extract the file name from a full path (e.g. C:\temp\image.gif). I need a regular expression to validate that the user has selected an suitable image to upload and that the path to it is a valid file path. Anyone here willing to provide some pointers? Cheers, Julian Voelcker The Virtual World (UK) Limited Cirencester, United Kingdom
| [aspngregexp] member Click here to reveal e-mail address = YOUR ID | http://www.asplists.com/asplists/aspngregexp.asp = JOIN/QUIT | http://www.asplists.com/search = SEARCH Archives
| [aspngregexp] member Click here to reveal e-mail address = YOUR ID | http://www.asplists.com/asplists/aspngregexp.asp = JOIN/QUIT | http://www.asplists.com/search = SEARCH Archives | [aspngregexp] member Click here to reveal e-mail address = YOUR ID | http://www.asplists.com/asplists/aspngregexp.asp = JOIN/QUIT | http://www.asplists.com/search = SEARCH Archives ------------------------------------------------------------- This email and any files transmitted are confidential and intended solely for the use of the individual or entity to which they are addressed, whose privacy should be respected. Any views or opinions are solely those of the author and do not necessarily represent those of the Trencor Group, or any of its representatives, unless specifically stated. Email transmission cannot be guaranteed to be secure, error free or without virus contamination. The sender therefore accepts no liability for any errors or omissions in the contents of this message, nor for any virus infection that might result from opening this message. Trencor is not responsible in the event of any third party interception of this email. If you have received this email in error please notify Click here to reveal e-mail address For more information about Trencor, visit www.trencor.net
|
|
| |
|
| |
| Wayne King (ASP.NET) |
Set your: RegularExpressionValidator.ValidationExpression =3D ".+\.(gif|jpe?g|bmp)"
That will validate that the file's extension is one of: gif, jpg, jpeg, bmp. However, it will not confirm that the file exists. Using standard HTML, there is no way (*should* be no way) to confirm on client-side -- before postback -- that a file path is valid. For security reasons, you cannot write client-side script that will scan a user's hard drive.
If you *really* want to be able to do this, then you'll need to have your users loosen their browser security settings. For instance in InternetExplorer, the default setting for "Initialize and script ActiveX controls not marked as safe" is "Disable". If this is set to enable, then you should be able to write client-side script that creates an instance of the Scripting.FileSystemObject, which in turn will allow you to confirm a file path.
To run this script (that would use the FileSystemObject), you would use the CustomValidator instead of (or possibly in addition to) the RegularExpressionValidator. The CustomValidator will call your custom validation function/script when validation is needed.
-Wayne This posting is provided "AS IS" with no warranties, and confers no rights.
-----Original Message----- From: Julian Voelcker [mailto:Click here to reveal e-mail address]=20 Sent: Tuesday, July 02, 2002 2:24 PM To: aspngregexp Subject: [aspngregexp] Re: Validating and extracting filenames
Thanks, but I would like to do the validation before they attempt to upload=20 the image by checking what is selected using the RegularExpression Validator.
How would I go about this using Regular Expressions Cheers,
Julian Voelcker The Virtual World (UK) Limited Cirencester, United Kingdom On Tue, 2 Jul 2002 14:02:25 -0500, Dave wanta wrote: [Original message clipped]
|
|
| |
|
| |
| Julian Voelcker |
Thanks Wayne that really helps.
I realise that you can't check a users systems, however I was thinking along the lines of a regular expression to check that the file path is correctly formatted.
For example, the file has to have a path - just a filename is a no no. It has to have either a valid drive name or UNC path.
Looking at Regexlib.com there appears to be some code to do this..
([A-Z]:\\[^/:\*\?<>\|]+\.\w{2,6})|(\\{2}[^/:\*\?<>\|]+\.\w{2,6})
Essentially I want to modify this to only handle the image files as previously, but don't know where to begin.
Cheers,
Julian Voelcker The Virtual World (UK) Limited Cirencester, United Kingdom
On Wed, 3 Jul 2002 13:32:13 -0700, Wayne King (ASP.NET) wrote: [Original message clipped]
|
|
| |
|
| |
| Wayne King (ASP.NET) |
That pattern is a little redundant. It restricts file extensions to 2 to 6 characters. It also doesn't allow lower-case drive letters. Here's a little cleaner version that should have the same semantics, and allows lower-case drive letters: ([A-Za-z]:\\|\\{2})[^/:*?<>|]+\.\w{2,6} or perhaps: ([A-Za-z]:|\\)\\[^/:*?<>|]+\.\w{2,6}
Now, to match only your legal file extensions, this should do it: ([A-Za-z]:|\\)\\[^/:*?<>|]+\.(gif|jpe?g|bmp)
These patterns allow the file and folder names to be composed of any characters that are not / : * ? < > |
Note the pattern will allow invalid paths like: C:\\foo.bmp C:\x\\y.bmp C:\..\...\x.bmp \\\x\y.bmp And potentially undesireable paths like: C:\x\.bmp
-----Original Message----- From: Julian Voelcker [mailto:Click here to reveal e-mail address]=20 Sent: Thursday, July 04, 2002 2:15 AM To: aspngregexp Subject: [aspngregexp] Re: Validating and extracting filenames
Thanks Wayne that really helps.
I realise that you can't check a users systems, however I was thinking=20 along the lines of a regular expression to check that the file path is=20 correctly formatted.
For example, the file has to have a path - just a filename is a no no. It has to have either a valid drive name or UNC path.
Looking at Regexlib.com there appears to be some code to do this..
([A-Z]:\\[^/:\*\?<>\|]+\.\w{2,6})|(\\{2}[^/:\*\?<>\|]+\.\w{2,6})
Essentially I want to modify this to only handle the image files as=20 previously, but don't know where to begin.
Cheers,
Julian Voelcker The Virtual World (UK) Limited Cirencester, United Kingdom
On Wed, 3 Jul 2002 13:32:13 -0700, Wayne King (ASP.NET) wrote: [Original message clipped]
[Original message clipped]
|
|
| |
|
| |
| Julian Voelcker |
Thanks Wayne, that is really handy, now to work out how to check to see if the path is valid.
Out of interest, do you have any recommendations of good resources for learning about regular expressions?
Cheers,
Julian Voelcker The Virtual World (UK) Limited Cirencester, United Kingdom On Fri, 5 Jul 2002 20:03:48 -0700, Wayne King (ASP.NET) wrote: [Original message clipped]
|
|
| |
|
| |
| Wayne King (ASP.NET) |
An excellent resource for learning about regular expressions is the book /Mastering Regular Expressions/ by Jeffrey Friedl and published by O'Reilly.
-----Original Message----- From: Julian Voelcker [mailto:Click here to reveal e-mail address]=20 Sent: Wednesday, July 10, 2002 2:04 AM To: aspngregexp Subject: [aspngregexp] Re: Validating and extracting filenames
Thanks Wayne, that is really handy, now to work out how to check to see if=20 the path is valid.
Out of interest, do you have any recommendations of good resources for=20 learning about regular expressions?
Cheers,
Julian Voelcker The Virtual World (UK) Limited Cirencester, United Kingdom On Fri, 5 Jul 2002 20:03:48 -0700, Wayne King (ASP.NET) wrote: [Original message clipped]
|
|
| |
|
|
|
|
|