How check filesize using FileUpload control?

Discussion in 'ASP .Net' started by VB Programmer, Jan 18, 2006.

  1. I am using a FileUpload control (ASP.NET 2.0). How do I check the length of
    the file in bytes BEFORE I let them upload it?

    I believe my code checks now AFTER...

    If FileUpload1.PostedFile.ContentLength >
    ConfigurationManager.AppSettings("MaxVideoUploadSize") Then
    lblErr.Text = "File needs to be > 0 bytes and less than " &
    ConfigurationManager.AppSettings("MaxVideoUploadSize") / 1000000 & " MB."
    Exit Sub
    End If
    VB Programmer, Jan 18, 2006
    #1
    1. Advertising

  2. VB Programmer

    Bruce Barker Guest

    you can't unless you write your own active/x control to do the uploads. also
    you can not catch file too large, as asp.net just terminates the upload
    (closes the socket) to stop at max size, thus no response can be sent.

    -- bruce (sqlwork.com)


    "VB Programmer" <> wrote in message
    news:...
    >I am using a FileUpload control (ASP.NET 2.0). How do I check the length
    >of the file in bytes BEFORE I let them upload it?
    >
    > I believe my code checks now AFTER...
    >
    > If FileUpload1.PostedFile.ContentLength >
    > ConfigurationManager.AppSettings("MaxVideoUploadSize") Then
    > lblErr.Text = "File needs to be > 0 bytes and less than " &
    > ConfigurationManager.AppSettings("MaxVideoUploadSize") / 1000000 & " MB."
    > Exit Sub
    > End If
    >
    >
    Bruce Barker, Jan 18, 2006
    #2
    1. Advertising

  3. My users are going to upload video files, up to 10MB each. What are my
    alternatives (FREE preferred)....

    "Bruce Barker" <> wrote in message
    news:...
    > you can't unless you write your own active/x control to do the uploads.
    > also you can not catch file too large, as asp.net just terminates the
    > upload (closes the socket) to stop at max size, thus no response can be
    > sent.
    >
    > -- bruce (sqlwork.com)
    >
    >
    > "VB Programmer" <> wrote in message
    > news:...
    >>I am using a FileUpload control (ASP.NET 2.0). How do I check the length
    >>of the file in bytes BEFORE I let them upload it?
    >>
    >> I believe my code checks now AFTER...
    >>
    >> If FileUpload1.PostedFile.ContentLength >
    >> ConfigurationManager.AppSettings("MaxVideoUploadSize") Then
    >> lblErr.Text = "File needs to be > 0 bytes and less than " &
    >> ConfigurationManager.AppSettings("MaxVideoUploadSize") / 1000000 & " MB."
    >> Exit Sub
    >> End If
    >>
    >>

    >
    >
    VB Programmer, Jan 18, 2006
    #3
  4. Hey y'all look what I found:
    https://msdn.microsoft.com/asp.net/default.aspx?pull=/library/en-us/dnaspp/html/UploadASP2.asp

    "VB Programmer" <> wrote in message
    news:...
    > My users are going to upload video files, up to 10MB each. What are my
    > alternatives (FREE preferred)....
    >
    > "Bruce Barker" <> wrote in message
    > news:...
    >> you can't unless you write your own active/x control to do the uploads.
    >> also you can not catch file too large, as asp.net just terminates the
    >> upload (closes the socket) to stop at max size, thus no response can be
    >> sent.
    >>
    >> -- bruce (sqlwork.com)
    >>
    >>
    >> "VB Programmer" <> wrote in message
    >> news:...
    >>>I am using a FileUpload control (ASP.NET 2.0). How do I check the length
    >>>of the file in bytes BEFORE I let them upload it?
    >>>
    >>> I believe my code checks now AFTER...
    >>>
    >>> If FileUpload1.PostedFile.ContentLength >
    >>> ConfigurationManager.AppSettings("MaxVideoUploadSize") Then
    >>> lblErr.Text = "File needs to be > 0 bytes and less than " &
    >>> ConfigurationManager.AppSettings("MaxVideoUploadSize") / 1000000 & "
    >>> MB."
    >>> Exit Sub
    >>> End If
    >>>
    >>>

    >>
    >>

    >
    >
    VB Programmer, Jan 18, 2006
    #4
  5. VB Programmer

    Bruce Barker Guest

    note: you can do the same 1.1

    asp.net fileupload support loads the file into memory, then calls page
    processing. as long as you have enough memeory (and up the page timeout),
    you can load 10mb files.

    there are third party controls that write the file to disk (in a temp
    folder) as it comes in to save memory, then passes the filename to the page.

    -- bruce (sqlwork.com)


    "VB Programmer" <> wrote in message
    news:%...
    > Hey y'all look what I found:
    > https://msdn.microsoft.com/asp.net/default.aspx?pull=/library/en-us/dnaspp/html/UploadASP2.asp
    >
    > "VB Programmer" <> wrote in message
    > news:...
    >> My users are going to upload video files, up to 10MB each. What are my
    >> alternatives (FREE preferred)....
    >>
    >> "Bruce Barker" <> wrote in message
    >> news:...
    >>> you can't unless you write your own active/x control to do the uploads.
    >>> also you can not catch file too large, as asp.net just terminates the
    >>> upload (closes the socket) to stop at max size, thus no response can be
    >>> sent.
    >>>
    >>> -- bruce (sqlwork.com)
    >>>
    >>>
    >>> "VB Programmer" <> wrote in message
    >>> news:...
    >>>>I am using a FileUpload control (ASP.NET 2.0). How do I check the
    >>>>length of the file in bytes BEFORE I let them upload it?
    >>>>
    >>>> I believe my code checks now AFTER...
    >>>>
    >>>> If FileUpload1.PostedFile.ContentLength >
    >>>> ConfigurationManager.AppSettings("MaxVideoUploadSize") Then
    >>>> lblErr.Text = "File needs to be > 0 bytes and less than " &
    >>>> ConfigurationManager.AppSettings("MaxVideoUploadSize") / 1000000 & "
    >>>> MB."
    >>>> Exit Sub
    >>>> End If
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
    Bruce Barker, Jan 18, 2006
    #5
  6. That doesn't say anything about checking the file size prior to uploading
    it.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    You can lead a fish to a bicycle,
    but it takes a very long time,
    and the bicycle has to *want* to change.

    "VB Programmer" <> wrote in message
    news:%...
    > Hey y'all look what I found:
    > https://msdn.microsoft.com/asp.net/default.aspx?pull=/library/en-us/dnaspp/html/UploadASP2.asp
    >
    > "VB Programmer" <> wrote in message
    > news:...
    >> My users are going to upload video files, up to 10MB each. What are my
    >> alternatives (FREE preferred)....
    >>
    >> "Bruce Barker" <> wrote in message
    >> news:...
    >>> you can't unless you write your own active/x control to do the uploads.
    >>> also you can not catch file too large, as asp.net just terminates the
    >>> upload (closes the socket) to stop at max size, thus no response can be
    >>> sent.
    >>>
    >>> -- bruce (sqlwork.com)
    >>>
    >>>
    >>> "VB Programmer" <> wrote in message
    >>> news:...
    >>>>I am using a FileUpload control (ASP.NET 2.0). How do I check the
    >>>>length of the file in bytes BEFORE I let them upload it?
    >>>>
    >>>> I believe my code checks now AFTER...
    >>>>
    >>>> If FileUpload1.PostedFile.ContentLength >
    >>>> ConfigurationManager.AppSettings("MaxVideoUploadSize") Then
    >>>> lblErr.Text = "File needs to be > 0 bytes and less than " &
    >>>> ConfigurationManager.AppSettings("MaxVideoUploadSize") / 1000000 & "
    >>>> MB."
    >>>> Exit Sub
    >>>> End If
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
    Kevin Spencer, Jan 18, 2006
    #6
  7. True. :)

    "Kevin Spencer" <> wrote in message
    news:...
    > That doesn't say anything about checking the file size prior to uploading
    > it.
    >
    > --
    > HTH,
    >
    > Kevin Spencer
    > Microsoft MVP
    > .Net Developer
    > You can lead a fish to a bicycle,
    > but it takes a very long time,
    > and the bicycle has to *want* to change.
    >
    > "VB Programmer" <> wrote in message
    > news:%...
    >> Hey y'all look what I found:
    >> https://msdn.microsoft.com/asp.net/default.aspx?pull=/library/en-us/dnaspp/html/UploadASP2.asp
    >>
    >> "VB Programmer" <> wrote in message
    >> news:...
    >>> My users are going to upload video files, up to 10MB each. What are my
    >>> alternatives (FREE preferred)....
    >>>
    >>> "Bruce Barker" <> wrote in message
    >>> news:...
    >>>> you can't unless you write your own active/x control to do the uploads.
    >>>> also you can not catch file too large, as asp.net just terminates the
    >>>> upload (closes the socket) to stop at max size, thus no response can be
    >>>> sent.
    >>>>
    >>>> -- bruce (sqlwork.com)
    >>>>
    >>>>
    >>>> "VB Programmer" <> wrote in message
    >>>> news:...
    >>>>>I am using a FileUpload control (ASP.NET 2.0). How do I check the
    >>>>>length of the file in bytes BEFORE I let them upload it?
    >>>>>
    >>>>> I believe my code checks now AFTER...
    >>>>>
    >>>>> If FileUpload1.PostedFile.ContentLength >
    >>>>> ConfigurationManager.AppSettings("MaxVideoUploadSize") Then
    >>>>> lblErr.Text = "File needs to be > 0 bytes and less than " &
    >>>>> ConfigurationManager.AppSettings("MaxVideoUploadSize") / 1000000 & "
    >>>>> MB."
    >>>>> Exit Sub
    >>>>> End If
    >>>>>
    >>>>>
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
    VB Programmer, Jan 18, 2006
    #7
  8. I think I figured out a workaround to solve this issue with checking the file
    size PRIOR to upload in ASP.NET 2.0. This will probably work in 1.1 as well.

    Let’s say one of your users is trying to upload a 15 MB file but the limit
    for upload is 10 MB. When you set the value for <httpRuntime
    maxRequestLength="value" /> in web.config, don’t set it at 10 MB but at
    something much higher like 20 MB. In your upload page, do a check for the
    ContentLength (convert this to KB by dividing it by 1024 and round it up by
    using System.Math.Ceiling) of the file against the 10 MB (10 x 1024 = 10240)
    limit inside the click event for the upload button. You should be able to
    trap the size limit check BEFORE the file is loaded onto the server.

    Protected Sub Upload_Click () Handles As Upload.Click
    If FileUpload1.PostedFile.ContentLength > 10240 then
    lblMessage.Text = “File is too large.â€
    Else
    …
    FileUpload1.PostedFile.SaveAs(Location)
    End If
    End Sub

    What’s happening here is that the 15 MB file will be loaded into the memory
    in IIS. But since the maxRequestLength limit is set at 20 MB, you don’t run
    into the “Can’t find page†problem. The click event should fire properly
    within the application and trap the error prior to saving the file on the
    server.

    I've tried this with a 50 MB setting on maxRequestLength with multiple
    FileUpload controls and it is working fine.

    Hope this helps.
    =?Utf-8?B?R29yZG9u?=, Jan 27, 2006
    #8
  9. Thanks Gordon!

    "Gordon" <> wrote in message
    news:...
    >I think I figured out a workaround to solve this issue with checking the
    >file
    > size PRIOR to upload in ASP.NET 2.0. This will probably work in 1.1 as
    > well.
    >
    > Let's say one of your users is trying to upload a 15 MB file but the limit
    > for upload is 10 MB. When you set the value for <httpRuntime
    > maxRequestLength="value" /> in web.config, don't set it at 10 MB but at
    > something much higher like 20 MB. In your upload page, do a check for the
    > ContentLength (convert this to KB by dividing it by 1024 and round it up
    > by
    > using System.Math.Ceiling) of the file against the 10 MB (10 x 1024 =
    > 10240)
    > limit inside the click event for the upload button. You should be able to
    > trap the size limit check BEFORE the file is loaded onto the server.
    >
    > Protected Sub Upload_Click () Handles As Upload.Click
    > If FileUpload1.PostedFile.ContentLength > 10240 then
    > lblMessage.Text = "File is too large."
    > Else
    > .
    > FileUpload1.PostedFile.SaveAs(Location)
    > End If
    > End Sub
    >
    > What's happening here is that the 15 MB file will be loaded into the
    > memory
    > in IIS. But since the maxRequestLength limit is set at 20 MB, you don't
    > run
    > into the "Can't find page" problem. The click event should fire properly
    > within the application and trap the error prior to saving the file on the
    > server.
    >
    > I've tried this with a 50 MB setting on maxRequestLength with multiple
    > FileUpload controls and it is working fine.
    >
    > Hope this helps.
    VB Programmer, Jan 27, 2006
    #9
  10. VB Programmer

    Joined:
    Aug 31, 2012
    Messages:
    11
    77777777777777777777777777777
    Last edited: Sep 1, 2012
    , Sep 1, 2012
    #10
  11. VB Programmer

    Joined:
    Aug 31, 2012
    Messages:
    11
    gfffffffffffffffffffffffffffffffffffffffffffffffffff
    , Sep 1, 2012
    #11
  12. VB Programmer

    Joined:
    Aug 31, 2012
    Messages:
    11
    Fake

    ffffffffffffffffffffffffffffffff
    ds
    sd
    f
    sdf
    s
    df
    sd
    fsd
    f
    sd
    Last edited: Sep 1, 2012
    , Sep 1, 2012
    #12
    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. Arjen

    Format filesize in kB or MB

    Arjen, Aug 8, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    2,131
    Jerry III
    Aug 9, 2003
  2. Magix

    check filesize before reading it

    Magix, Jul 5, 2004, in forum: C Programming
    Replies:
    4
    Views:
    346
    Dan Pop
    Jul 6, 2004
  3. Alan Howard
    Replies:
    2
    Views:
    292
    Alan Howard
    Jun 20, 2004
  4. trapman

    get filesize using javascript

    trapman, Apr 19, 2006, in forum: Javascript
    Replies:
    3
    Views:
    540
    Randy Webb
    Apr 19, 2006
  5. linbox
    Replies:
    0
    Views:
    118
    linbox
    Jan 20, 2007
Loading...

Share This Page