Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FileUpload/regularexpressionvalidator 1

Status
Not open for further replies.

abig99

Programmer
Oct 22, 2001
53
GB
Hi all, mostly i do VBA programming so this is somewhat new to me, ive set up a page with a fileupload control on it, however, my regularexpressionvalidator doesnt work correctly, after uploading the first zip file, the regularexpression error message doesnt clear itself, by using a response.write(File1.hasfile) in the onload event of this page it appears as though the FileUpload control doesnt empty itself correctly after upload completion, how would i go about doing this? And what exactly part of the fileupload function does regularexpressionvalidator check? I assumed it was FileName, but thats not the case as that part does clear.

Cheers.


############## CODE START ##################
<table border="0" width="600" align=center>
<tr>
<td>
<asp:FileUpload EnableViewState=false id=File1 runat="server" />
</td>
</tr>
<tr><td>
<asp:Button id="Submit1" runat="server" OnClick="Button1_Click" Text="Upload File" />
</td>
</tr>
</table>
<br />
<div align=left>
<asp:RequiredFieldValidator Display=Dynamic id="RegularExpressionValidator2" runat="server" text="<b>Note: You must provide a file path!</b>" ControlToValidate="File1" />
<asp:RegularExpressionValidator Display=dynamic id="RegularExpressionValidator1" runat="server" text="<b>Note: Only .zip files are allowed!</b>" ValidationExpression="(^([\w*\s*\d*\(*\)*\\*_*\-*]*)$|^((([a-zA-Z]:)|
(\\{2}\w+)\$?)(\\(\w([\w*\s*\d*\(*\)*\\*_*\-*]*)))+(.zip|.ZIP))$)" ControlToValidate="File1"></asp:RegularExpressionValidator>

####################### CODE END ######################
 
="(^([\w*\s*\d*\(*\)*\\*_*\-*]*)$|^((([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w([\w*\s*\d*\(*\)*\\*_*\-*]*)))+(.zip|.ZIP))

Should be

="(|^((([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w([\w*\s*\d*\(*\)*\\*_*\-*]*)))+(.zip|.ZIP))

Was messing about thinking it was my regex that was faulty.
 
No pipe either.

="(^((([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w([\w*\s*\d*\(*\)*\\*_*\-*]*)))+(.zip|.ZIP))
 
OK, i found the problem myself.

Basically the regular expression microsoft give you on their examples doesnt work correctly with the fileupload control.

In my case i used.

=".*(.zip|.ZIP)$"

This checks the last 4 characters are .zip or .ZIP, which works fine. If your worried about .exe.zip files then just use your own naming schema when saving the files.
 
You could simply do somthing like this in the code behind as well:
Code:
Dim FileName As String = File1.FileName
Dim Extention As String = Path.GetExtension(FileName)

If Extention.ToLower() = ".zip" Then
...Do something
Else
...do something else
End IF
Jim
 
Hi Jim, thanks for your reply, where would I place that code for it to run before page submission, i have something similar on the submit button but alas thats server side checking?

To my eyes that looks like server side code, which would require you to submit the page (if its a big file the user would be sat there waiting only to be told the extension is of the wrong type), in this instance I wanted to tell the user before they submit the page, hence the regularexpressionvalidator.

Cheers.

Hayden
 
I use that code in a custom validator control.

So something like:
Button click event:
If Page.IsValid And Page.IsPostBack Then
... do your save...

custUpload_ServerValidate event...
args.IsValid = True
...code I gave you before.

So, it is server side, and does a post back, but till not upload until the args.IsValid = True.. so the user is not waiting, then getting an error message

Jim

 
Hi Jim,

Ok, I tried what you suggested to see if it worked, you can do the same by pasting the code below into a blank aspx page. If I attempt to check a 150mb file server side it will actually place the whole page into the servers memory before doing server side checking. If you did this a different way pls let me know. Thanks

<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
<head>

<script runat="server">

Sub ValidateBtn_OnClick(sender As Object, e As EventArgs)

' Display whether the page passed validation.
If Page.IsValid Then

Message.Text = "Page is valid."

Else

Message.Text = "Page is not valid!"

End If

End Sub

Sub ServerValidation(source As Object, args As ServerValidateEventArgs)

Try

If Text1.PostedFile.ContentLength > 100 Then
CustomValidator1.ErrorMessage = "File too large"
args.IsValid = False
Else
args.IsValid = True
End If
Catch ex As Exception

args.IsValid = false

End Try

End Sub

</script>

</head>
<body>

<form id="Form1" runat="server">

<h3>CustomValidator ServerValidate Example</h3>

<asp:Label id="Message"
Text="Enter an even number:"
Font-Name="Verdana"
Font-Size="10pt"
runat="server"/>

<p>

<asp:FileUpload id="Text1"
runat="server" />

&nbsp;&nbsp;

<asp:CustomValidator id="CustomValidator1"
ControlToValidate="Text1"
ClientValidationFunction="ClientValidate"
OnServerValidate="ServerValidation"
Display="Static"
ErrorMessage="Incorrect File Type"
ForeColor="green"
Font-Name="verdana"
Font-Size="10pt"
runat="server"/>

<p>

<asp:Button id="Button1"
Text="Validate"
OnClick="ValidateBtn_OnClick"
runat="server"/>

</form>

</body>
</html>

<script language="vbscript">

<!--

Sub ClientValidate(source, arguments)

If right(arguments.value,4) = ".zip" Then
arguments.IsValid=true
Else
arguments.IsValid=false
End If

End Sub

' -->

</script>


Any feedback appreciated.

Hayden
 
What I gave you is the way I do it. The problem with your code is you are checking the length of the file which will cause a problem as you said. In your original post you said you wanted to check for a .zip extention which is what i gave you.
 
Guess im getting confused, you said checking if the file is a .zip serverside would not upload the file to the server, but looking at my example it does, ignore the fact im checking for length, you could replace that with .zip and it would still submit the file to the server i believe.

 
you could replace that with .zip and it would still submit the file to the server I believe.
Give it a try. I don't belive it will. Let me know.
 
Hi Jim,

I changed my code to run your suggested code, still seems to submit the page inclusive of the file.

<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.IO" %>
<html>
<head>

<script runat="server">

Sub ValidateBtn_OnClick(sender As Object, e As EventArgs)

' Display whether the page passed validation.
If Page.IsValid Then

Message.Text = "Page is valid."

Else

Message.Text = "Page is not valid!"

End If

End Sub

Sub ServerValidation(source As Object, args As ServerValidateEventArgs)

Try
Dim FileName As String = Text1.FileName
Dim Extention As String = Path.GetExtension(FileName)

If Extention.ToLower() = ".zip" Then
args.IsValid = True
Else
CustomValidator1.ErrorMessage = ".zip test serverside"
args.IsValid = False
End If
Catch ex As Exception

args.IsValid = False

End Try

End Sub

</script>

</head>
<body>

<form id="Form1" runat="server">

<h3>CustomValidator ServerValidate Example</h3>

<asp:Label id="Message"
Text="Enter an even number:"
Font-Size="10pt"
runat="server"/>

<p>

<asp:FileUpload id="Text1"
runat="server" />

&nbsp;&nbsp;

<asp:CustomValidator id="CustomValidator1"
ControlToValidate="Text1"
ClientValidationFunction="ClientValidate"
OnServerValidate="ServerValidation"
Display="Static"
ErrorMessage=".zip test client side"
ForeColor="green"
Font-Size="10pt"
runat="server"/>

<p>

<asp:Button id="Button1"
Text="Validate"
OnClick="ValidateBtn_OnClick"
runat="server"/>

</form>

</body>
</html>

<script language="vbscript">

<!--

Sub ClientValidate(source, arguments)

If right(arguments.value,2) = "ip" Then
arguments.IsValid=true
Else
arguments.IsValid=false
End If

End Sub

' -->

</script>
 
Are you using a large file? Its pretty quick untill you get into the 100mb+ range, then you can really notice the upload time.
 
OK, well thanks for the help Jim, completely confused about this now :(

Will keep searching.
 
Yeah.. sorry.. wish I could be more help.. let us know if you find anyting..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top