Space in fileupload control

H

HIK

I have a problem with the fileupload control. If the user inadvertantly
puts a space in the control, the page will not fire. I tried to trap
the error with a regular expression validator, but it apparently also
doesn't fire when there is a space in the control. Is there anyway to
catch this error and warn the user, or erase it?

Haim
 
W

Walter Wang [MSFT]

Hi Haim,

The form is not submitted because by default IE will not allow it if the
file upload control contains invalid path (it doesn't mean that the file
doesn't exist, just mean invalid path format, for exmaple, a space is
inserted at the front of the file name).

You can check this using a RegularExpressionValidator:

<form id="form1" runat="server">
<div>
<asp:FileUpload ID="file1" runat="server" />
<asp:Button ID="button1" Text="Upload" runat="server" />
<asp:RegularExpressionValidator ID="val1" runat="server"
ControlToValidate="file1" EnableClientScript="true" ErrorMessage="Please
make sure valid file"
ValidationExpression="^[^\s].*"></asp:RegularExpressionValidator>
</div>
</form>

You can test this by:
1) Select a file by using the "Browse..." button of the file upload control
2) Insert a space at the front of the file name of the file upload control;
press TAB twice to cause it lose focus; you will notice the validator
failed and its error message is shown.

Please test it and tell me the result. If your code doesn't work, please
create a reproducible project and post it here or send it to me. Thank you.

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
H

HIK

Dear Walter,

Thank you for your reply. I have tried using regular expressions. Your
suggestion doesn't seem to catch anything. The one I am using is the
following:
^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.doc|.DOC|.rtf|.RTF|.wpd|.WPD|.htm|.HTM|.html|.HTML)$
which works well in most cases. for example [c:\abc.com] is valid and
[ c:\abc.com] is not valid.
But if the user inadvertantly types a space in the fileupload textbox
without clicking on the browse button,the validator does not fire. Even
if he clicks the browse button, types a space, clicks cancel and
continues, the space remains in the textbox and no validation is done.
The validation control does not show, and the user cannot see the space
that was inadvertantly put in the box.

Haim
Hi Haim,

The form is not submitted because by default IE will not allow it if the
file upload control contains invalid path (it doesn't mean that the file
doesn't exist, just mean invalid path format, for exmaple, a space is
inserted at the front of the file name).

You can check this using a RegularExpressionValidator:

<form id="form1" runat="server">
<div>
<asp:FileUpload ID="file1" runat="server" />
<asp:Button ID="button1" Text="Upload" runat="server" />
<asp:RegularExpressionValidator ID="val1" runat="server"
ControlToValidate="file1" EnableClientScript="true" ErrorMessage="Please
make sure valid file"
ValidationExpression="^[^\s].*"></asp:RegularExpressionValidator>
</div>
</form>

You can test this by:
1) Select a file by using the "Browse..." button of the file upload control
2) Insert a space at the front of the file name of the file upload control;
press TAB twice to cause it lose focus; you will notice the validator
failed and its error message is shown.

Please test it and tell me the result. If your code doesn't work, please
create a reproducible project and post it here or send it to me. Thank you.

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

Hi Haim,

You can call a javascript function to validate the file upload control
before submitting the form:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs"
Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript" language="javascript">
function myPostback()
{
var re =
/^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.doc|.DOC|.rtf|.RTF|.wpd|.WPD|.h
tm|.HTM|.html|.HTML)$/;
var m = re.exec(form1.file1.value);
if (m != null) {
alert(m[0]);
return true;
} else {
alert('not match');
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="file1" runat="server" />
<asp:Button ID="button1" OnClientClick="return myPostback();"
Text="Upload" runat="server" />
</div>
</form>

</body>
</html>

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top