Space in fileupload control

Discussion in 'ASP .Net Web Controls' started by HIK, Sep 12, 2006.

  1. HIK

    HIK Guest

    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
    HIK, Sep 12, 2006
    #1
    1. Advertising

  2. 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 (, 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.
    Walter Wang [MSFT], Sep 13, 2006
    #2
    1. Advertising

  3. HIK

    HIK Guest

    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

    Walter Wang [MSFT] wrote:

    > 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 (, 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.
    >
    HIK, Sep 14, 2006
    #3
  4. 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 (, 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.
    Walter Wang [MSFT], Sep 14, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,890
    Bryan Bullard
    Jul 11, 2003
  2. Christian Seberino
    Replies:
    21
    Views:
    1,652
    Stephen Horne
    Oct 27, 2003
  3. Ian Bicking
    Replies:
    2
    Views:
    1,009
    Steve Lamb
    Oct 23, 2003
  4. Ian Bicking
    Replies:
    2
    Views:
    721
    Michael Hudson
    Oct 24, 2003
  5. Nathan Sokalski
    Replies:
    1
    Views:
    743
    Mark Rae
    Jan 11, 2007
Loading...

Share This Page