File Size

Discussion in 'ASP .Net' started by rn5a@rediffmail.com, Feb 18, 2007.

  1. Guest

    I am giving users the option to upload files from their hard disk to
    the server but only those files will be uploaded whose size is less
    tha or equal to 1 MB.

    I am using the ASP.NET FileUpload control to design the interface.
    This is the code:

    <script runat="server">
    Sub Page_Load(.....)
    If (Page.IsPostBack) Then
    If (fudFile1.FileName <> "") Then
    If (fudFile1.FileBytes.Length / 1048576 > 1) Then
    lblMessage.Text = "File size cannot exceed 1 MB"
    Else
    fudFile1.SaveAs(Server.MapPath(fudFile1.FileName))
    lblMessage.Text = "File uploaded"
    End If
    End If
    End If
    End Sub
    </script>

    <form runat="server">
    <asp:FileUpload ID="fudFile1" runat="server"/><br><br>
    <asp:Button ID="btnUpload" Text="UPLOAD FILE" runat="server"/>
    <asp:Label ID="lblMessage" runat="server"/>
    </form>

    Now what I find is if I try to upload a file whose size is greater
    than 1 MB but less than or equal to 4 MB, then, as expected, the Label
    renders the message "File size cannot exceed 1 MB" but if the file
    size exceeds 4 MB, then the Label doesn't render the "File size cannot
    exceed 1 MB" message.

    Instead, IE just renders the omnipresent "The page cannot be
    displayed" page with the error "Cannot find server or DNS Error" at
    the very end of the page.

    Why isn't the Label displaying the "File size cannot exceed 1 MB"
    message when the file size is greater than 1 MB but less than or equal
    to 4 MB? How do I ensure that even if the file size exceeds 4 MB,
    users are still shown the Label with the message "File size cannot
    exceed 1 MB"?
     
    , Feb 18, 2007
    #1
    1. Advertising

  2. Patrick Guest

    Try If fudFile1.PostedFile.ContentLength > 4194304

    <> wrote in message
    news:...
    >I am giving users the option to upload files from their hard disk to
    > the server but only those files will be uploaded whose size is less
    > tha or equal to 1 MB.
    >
    > I am using the ASP.NET FileUpload control to design the interface.
    > This is the code:
    >
    > <script runat="server">
    > Sub Page_Load(.....)
    > If (Page.IsPostBack) Then
    > If (fudFile1.FileName <> "") Then
    > If (fudFile1.FileBytes.Length / 1048576 > 1) Then
    > lblMessage.Text = "File size cannot exceed 1 MB"
    > Else
    > fudFile1.SaveAs(Server.MapPath(fudFile1.FileName))
    > lblMessage.Text = "File uploaded"
    > End If
    > End If
    > End If
    > End Sub
    > </script>
    >
    > <form runat="server">
    > <asp:FileUpload ID="fudFile1" runat="server"/><br><br>
    > <asp:Button ID="btnUpload" Text="UPLOAD FILE" runat="server"/>
    > <asp:Label ID="lblMessage" runat="server"/>
    > </form>
    >
    > Now what I find is if I try to upload a file whose size is greater
    > than 1 MB but less than or equal to 4 MB, then, as expected, the Label
    > renders the message "File size cannot exceed 1 MB" but if the file
    > size exceeds 4 MB, then the Label doesn't render the "File size cannot
    > exceed 1 MB" message.
    >
    > Instead, IE just renders the omnipresent "The page cannot be
    > displayed" page with the error "Cannot find server or DNS Error" at
    > the very end of the page.
    >
    > Why isn't the Label displaying the "File size cannot exceed 1 MB"
    > message when the file size is greater than 1 MB but less than or equal
    > to 4 MB? How do I ensure that even if the file size exceeds 4 MB,
    > users are still shown the Label with the message "File size cannot
    > exceed 1 MB"?
    >
     
    Patrick, Feb 18, 2007
    #2
    1. Advertising

  3. You get the file size on server side, that is after the file has already
    been uploaded. Files larger than 4M fail to upload and this causes your IE
    error. I am not aware of any simple ways of evaluating file size on client
    side.

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    <> wrote in message
    news:...
    >I am giving users the option to upload files from their hard disk to
    > the server but only those files will be uploaded whose size is less
    > tha or equal to 1 MB.
    >
    > I am using the ASP.NET FileUpload control to design the interface.
    > This is the code:
    >
    > <script runat="server">
    > Sub Page_Load(.....)
    > If (Page.IsPostBack) Then
    > If (fudFile1.FileName <> "") Then
    > If (fudFile1.FileBytes.Length / 1048576 > 1) Then
    > lblMessage.Text = "File size cannot exceed 1 MB"
    > Else
    > fudFile1.SaveAs(Server.MapPath(fudFile1.FileName))
    > lblMessage.Text = "File uploaded"
    > End If
    > End If
    > End If
    > End Sub
    > </script>
    >
    > <form runat="server">
    > <asp:FileUpload ID="fudFile1" runat="server"/><br><br>
    > <asp:Button ID="btnUpload" Text="UPLOAD FILE" runat="server"/>
    > <asp:Label ID="lblMessage" runat="server"/>
    > </form>
    >
    > Now what I find is if I try to upload a file whose size is greater
    > than 1 MB but less than or equal to 4 MB, then, as expected, the Label
    > renders the message "File size cannot exceed 1 MB" but if the file
    > size exceeds 4 MB, then the Label doesn't render the "File size cannot
    > exceed 1 MB" message.
    >
    > Instead, IE just renders the omnipresent "The page cannot be
    > displayed" page with the error "Cannot find server or DNS Error" at
    > the very end of the page.
    >
    > Why isn't the Label displaying the "File size cannot exceed 1 MB"
    > message when the file size is greater than 1 MB but less than or equal
    > to 4 MB? How do I ensure that even if the file size exceeds 4 MB,
    > users are still shown the Label with the message "File size cannot
    > exceed 1 MB"?
    >
     
    Eliyahu Goldin, Feb 18, 2007
    #3
  4. Mark Rae Guest

    "Eliyahu Goldin" <> wrote in
    message news:...

    > I am not aware of any simple ways of evaluating file size on client side.


    I'm pretty sure the only options are a Java applet or an ActiveX control...
     
    Mark Rae, Feb 18, 2007
    #4
  5. It can be done with Javascript, but only if the browser local security is
    low enough to allow it. Its probably OK for intranets, but not for an
    internet scenario.

    This example is quite comprehensive but its a bit old now. Should still be
    a good pointer.

    http://www.faqts.com/knowledge_base/view.phtml/aid/1685

    --
    --
    Regards

    John Timney (MVP)
    http://www.johntimney.com
    http://www.johntimney.com/blog






    "Mark Rae" <> wrote in message
    news:...
    > "Eliyahu Goldin" <> wrote in
    > message news:...
    >
    >> I am not aware of any simple ways of evaluating file size on client side.

    >
    > I'm pretty sure the only options are a Java applet or an ActiveX
    > control...
    >
     
    John Timney \(MVP\), Feb 18, 2007
    #5
  6. Mark Rae Guest

    "John Timney (MVP)" <> wrote in message
    news:...

    > It can be done with Javascript, but only if the browser local security is
    > low enough to allow it. Its probably OK for intranets, but not for an
    > internet scenario.
    >
    > This example is quite comprehensive but its a bit old now. Should still
    > be a good pointer.
    >
    > http://www.faqts.com/knowledge_base/view.phtml/aid/1685


    Well, yes, but that's using an ActiveX object - specifically
    FileSystemObject - so no use for anything other than IE...
     
    Mark Rae, Feb 18, 2007
    #6
  7. If I recall it, it works in both IE and Netscape et al as it taps into Java
    objects for non IE browsers but it has to be ran as a trusted script. Hence
    my caveat that it was no good for internet. I'd only ever look to use
    something as quirky as this in an extreme case.

    Regards

    John Timney (MVP)
    http://www.johntimney.com
    http://www.johntimney.com/blog


    "Mark Rae" <> wrote in message
    news:...
    > "John Timney (MVP)" <> wrote in message
    > news:...
    >
    >> It can be done with Javascript, but only if the browser local security is
    >> low enough to allow it. Its probably OK for intranets, but not for an
    >> internet scenario.
    >>
    >> This example is quite comprehensive but its a bit old now. Should still
    >> be a good pointer.
    >>
    >> http://www.faqts.com/knowledge_base/view.phtml/aid/1685

    >
    > Well, yes, but that's using an ActiveX object - specifically
    > FileSystemObject - so no use for anything other than IE...
    >
     
    John Timney \(MVP\), Feb 18, 2007
    #7
  8. On in any case where the users trust the server. For example, medical
    practices providing services to their doctors.

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "John Timney (MVP)" <> wrote in message
    news:...
    > If I recall it, it works in both IE and Netscape et al as it taps into
    > Java objects for non IE browsers but it has to be ran as a trusted script.
    > Hence my caveat that it was no good for internet. I'd only ever look to
    > use something as quirky as this in an extreme case.
    >
    > Regards
    >
    > John Timney (MVP)
    > http://www.johntimney.com
    > http://www.johntimney.com/blog
    >
    >
    > "Mark Rae" <> wrote in message
    > news:...
    >> "John Timney (MVP)" <> wrote in message
    >> news:...
    >>
    >>> It can be done with Javascript, but only if the browser local security
    >>> is low enough to allow it. Its probably OK for intranets, but not for
    >>> an internet scenario.
    >>>
    >>> This example is quite comprehensive but its a bit old now. Should still
    >>> be a good pointer.
    >>>
    >>> http://www.faqts.com/knowledge_base/view.phtml/aid/1685

    >>
    >> Well, yes, but that's using an ActiveX object - specifically
    >> FileSystemObject - so no use for anything other than IE...
    >>

    >
    >
     
    Eliyahu Goldin, Feb 18, 2007
    #8
    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. tiewknvc9
    Replies:
    6
    Views:
    668
    Chris Uppal
    Oct 1, 2006
  2. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,605
    Michael Jung
    May 25, 2008
  3. Keith Thompson

    Re: File Size - Big File Size

    Keith Thompson, Oct 1, 2009, in forum: C Programming
    Replies:
    6
    Views:
    296
    Phil Carmody
    Oct 3, 2009
  4. Michael Tsang

    Re: File Size - Big File Size

    Michael Tsang, Oct 4, 2009, in forum: C Programming
    Replies:
    2
    Views:
    324
    Keith Thompson
    Oct 4, 2009
  5. Nobody

    Re: File Size - Big File Size

    Nobody, Oct 5, 2009, in forum: C Programming
    Replies:
    10
    Views:
    2,038
    Flash Gordon
    Oct 10, 2009
Loading...

Share This Page